reactjs - 使用redux成功提交Formik表单后如何清除表单
问题描述
我目前正在开发一个使用 Redux 和 Redux-saga 的 React 项目。我想知道如何在成功完成异步 api 调用后重置表单。使用 redux 和 redux saga 我使用的是
resetform({})
但它会在 api 成功执行之前立即重置表单
解决方案
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.
推荐阅读
- c++ - C ++上的char数组输出中的不同颜色字符
- c# - 格式化 C# 字符串以匹配特定结果
- javascript - 谷歌地图标记悬停效果
- javascript - 成功完整提交时弹出消息
- c++ - 在不释放所有动态分配的资源的情况下结束程序是否有风险?
- mysql-workbench - 显示数据库关系结构
- scala - 为 Apache Storm Bolt 编写单元测试时创建 testTuple 时出错
- android - 无法将字符串 Json 解析为 Json 对象
- json - Laravel 5.5 json 验证规则失败
- javascript - React Native - “未定义不是对象(评估'MyApp.navigation.openDrawer')