【问题标题】:How to update component state from withFormik of Formik如何从 Formik 的 withFormik 更新组件状态
【发布时间】:2020-05-12 00:19:36
【问题描述】:

我有Form,我在其中使用Formik's withFormik 钩子。我的问题是如何根据可以显示加载器的handleSubmit 函数更新加载器状态。

const [loading, setLoading] = React.useState(false);
.....
.....
<Button
  className={classes.button}
  type="submit"
  color="primary"
  variant="contained"
  disabled={loading}
>
  submit
 {loading && <CircularProgress size={24} className={classes.buttonProgress} />}
</Button>

.......
.......

handleSubmit: (values, { setSubmitting, setStatus }) => {
    setTimeout(() => {
        setLoading(false);
        // submit to the server
        alert(JSON.stringify(values, null, 2));
        setSubmitting(false);
        setStatus(true);
    }, 1000);
}

【问题讨论】:

    标签: reactjs formik


    【解决方案1】:

    您应该使用传入FormikContext.Provider 作为值属性的useEffect 和FormikContextShape。

    这是一个全面的多合一用法示例代码: https://gist.github.com/tgriesser/fdd08da075fec80328792319606c76e5

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-08
      • 2019-08-29
      • 1970-01-01
      • 1970-01-01
      • 2019-06-03
      • 2020-05-10
      • 2018-04-20
      相关资源
      最近更新 更多