首页 > 解决方案 > 使用redux成功提交Formik表单后如何清除表单

问题描述

我目前正在开发一个使用 Redux 和 Redux-saga 的 React 项目。我想知道如何在成功完成异步 api 调用后重置表单。使用 redux 和 redux saga 我使用的是

resetform({})

但它会在 api 成功执行之前立即重置表单

标签: reactjsreduxredux-sagaformik

解决方案


What I will do :

  • Make the call to the api,
  • with Redux, you can update in the store an entry like : isSubmitted which is a boolean.
  • after this, just check the props isSubmitted and when it's true just call the resetForm();
  • You can check the props with an effect (useEffect) or if you use an older version of react, componentDidUpdate.

According to the doc of Formik :

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

Hope it helps.


推荐阅读