英文:
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) => {
sendFormRequest(values, actions);
},
});
return (
<form className='form-container' onSubmit={formik.handleSubmit}>
{/* 1. Personal Info */}
{module === 0 &&
<PersonalInfo
formik={formik}
formStep={formStep}
setFormStep={handleSetFormStep}
/>}
{/* 2. Additional Info */}
{module === 1 &&
<AdditionalInfo
formik={formik}
formStep={formStep}
setModule={handleSetModule}
setFormStep={handleSetFormStep}
/>}
{/* 3. Job Info */}
{module === 2 &&
<JobInfo
formik={formik}
formStep={formStep}
setModule={handleSetModule}
setFormStep={handleSetFormStep}
/>}
{/* 4. Product Info */}
{module === 3 &&
<ProductInfo
formik={formik}
formStep={formStep}
setModule={handleSetModule}
setFormStep={handleSetFormStep}
isSubmitting={isSubmitting}
/>}
</form>
)
}
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(() => {
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(() => {
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) => 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])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论