简体   繁体   English

使用redux成功提交Formik表单后如何清除表单

[英]How to clear form after successfully submitting Formik form using redux

I am currently working on a React project which uses Redux and Redux-saga.我目前正在开发一个使用 Redux 和 Redux-saga 的 React 项目。 I am wondering how to make the form reset after successfully completing the async api call.我想知道如何在成功完成异步 api 调用后重置表单。 using redux and redux saga am I using the使用 redux 和 redux saga 我在使用

resetform({})

but it immediately resets the form before api is successfully executed但它会在 api 成功执行之前立即重置表单

What I will do :我将要做的 :

  • Make the call to the api,调用api,
  • with Redux, you can update in the store an entry like : isSubmitted which is a boolean.使用 Redux,您可以在 store 中更新一个条目,例如: isSubmitted ,这是一个布尔值。
  • after this, just check the props isSubmitted and when it's true just call the resetForm();在此之后,只需检查道具 isSubmitted 并且当它为true只需调用 resetForm();
  • You can check the props with an effect (useEffect) or if you use an older version of react, componentDidUpdate.您可以使用效果(useEffect)检查道具,或者如果您使用旧版本的react,componentDidUpdate。

According to the doc of Formik :根据Formik的文档:

The latter is useful for calling resetForm within componentDidUpdate or useEffect.

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM