What is the best way to save Formik values in Redux Toolkit so that I can persist them with redux-persist?

huangapple go评论90阅读模式
英文:

What is the best way to save Formik values in Redux Toolkit so that I can persist them with redux-persist?

问题

以下是翻译的代码部分:

export default function UserDetailsForm() {
  const formik = useFormik({
    initialValues: FormInitialValues, // 初始值
    validationSchema: FORM_MODULES[module]?.schema[formStep], // 验证模式
    onSubmit: (values, actions) => { // 提交时的操作
      sendFormRequest(values, actions); // 发送表单请求
    },
  });

  return (
    <form className='form-container' onSubmit={formik.handleSubmit}>
      {/* 1. 个人信息 */}
      {module === 0 &&
        <PersonalInfo
          formik={formik}
          formStep={formStep}
          setFormStep={handleSetFormStep}
        />
      }

      {/* 2. 附加信息 */}
      {module === 1 &&
        <AdditionalInfo
          formik={formik}
          formStep={formStep}
          setModule={handleSetModule}
          setFormStep={handleSetFormStep}
        />
      }

      {/* 3. 工作信息 */}
      {module === 2 &&
        <JobInfo
          formik={formik}
          formStep={formStep}
          setModule={handleSetModule}
          setFormStep={handleSetFormStep}
        />
      }

      {/* 4. 产品信息 */}
      {module === 3 &&
        <ProductInfo
          formik={formik}
          formStep={formStep}
          setModule={handleSetModule}
          setFormStep={handleSetFormStep}
          isSubmitting={isSubmitting}
        />
      }
    </form>
  )
}

希望对你有所帮助。

英文:

I have a multi step form made in React with formik and I am also using Redux Toolkit with redux-persist. I am able to save the form steps in the Redux state and the user remains in the current step even if they close/open the window again. But how can I also store the formik values he has entered in the Redux state so that the values persist as well?

This is the code of my form:

export default function UserDetailsForm() {
  const formik = useFormik({
    initialValues: FormInitialValues,
    validationSchema: FORM_MODULES[module]?.schema[formStep],
    onSubmit: (values, actions) =&gt; {
      sendFormRequest(values, actions);
    },
  });

  return (
        &lt;form className=&#39;form-container&#39; onSubmit={formik.handleSubmit}&gt;


          {/* 1. Personal Info */}

          {module === 0 &amp;&amp;
          &lt;PersonalInfo
            formik={formik}
            formStep={formStep}
            setFormStep={handleSetFormStep}
          /&gt;}

          {/* 2. Additional Info */}

          {module === 1 &amp;&amp;
          &lt;AdditionalInfo
            formik={formik}
            formStep={formStep}
            setModule={handleSetModule}
            setFormStep={handleSetFormStep}
          /&gt;}

          {/* 3. Job Info */}

          {module === 2 &amp;&amp;
          &lt;JobInfo
            formik={formik}
            formStep={formStep}
            setModule={handleSetModule}
            setFormStep={handleSetFormStep}
          /&gt;}

          {/* 4. Product Info */}

          {module === 3 &amp;&amp;
          &lt;ProductInfo
            formik={formik}
            formStep={formStep}
            setModule={handleSetModule}
            setFormStep={handleSetFormStep}
            isSubmitting={isSubmitting}
          /&gt;}

        &lt;/form&gt;
  )
}

Thanks in advance.

答案1

得分: 1

首先为您的草稿数值创建一个状态切片,然后您可以监视来自 useFormik 钩子返回的数值变化并触发保存它们的操作。

useEffect(() => {
	dispatch(saveDraftValues(formik.values));
}, [formik.values]);

您可以将 step 作为依赖项添加,以便仅在完成一步时保存草稿值,以避免在表单更改时触发多次操作。

useEffect(() => {
	dispatch(saveDraftValues(formik.values));
}, [step]);

然后,您可以从存储中获取值,例如 formDraft 切片,如果它不为 null,则使用草稿值设置您的初始值。

const dispatch = useDispatch();
const formDraft = useSelector((state: RootState) => state.formDraft.value);
const formik = useFormik({
	initialValues: formDraft || FormInitialValues,
	validationSchema: FORM_MODULES[module]?.schema[formStep],
	onSubmit: (values, actions) => {
		sendFormRequest(values, actions);
	},
});

useEffect(() => {
	dispatch(saveDraftValues(formik.values));
}, [step]);

希望这对您有所帮助。

英文:

First create a state slice for your draft values then you can watch values changes returned from useFormik hooks and trigger an action to save them.

useEffect(() =&gt; {
	dispatch(saveDraftValues(formik.values)) 
}, [formik.values])

You can add the step as dependency to save the draft values only when a step is finished to avoid triggering multiple action on form changes

useEffect(() =&gt; {
	dispatch(saveDraftValues(formik.values)) 
}, [step])

Then you can get the value from the store, for instance formDraft slice and if it's not null set your initial values with the draft values

const dispatch = useDispatch()
const formDraft = useSelector((state: RootState) =&gt; state.formDraft.value);
const formik = useFormik({
 initialValues: formDraft || FormInitialValues,
 validationSchema: FORM_MODULES[module]?.schema[formStep],
 onSubmit: (values, actions) =&gt; {
   sendFormRequest(values, actions);
 },
});

useEffect(() =&gt; {
 dispatch(saveDraftValues(formik.values)) 
}, [step])

huangapple
  • 本文由 发表于 2023年3月10日 01:02:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687781.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定