简体   繁体   English

React Formik:IsSubmitting 不适合我

[英]React Formik: IsSubmitting not working for me

I am using Formik in my react application.我在我的 React 应用程序中使用 Formik。 I tried to change the state of button enbales/disabled using isSubmitting property but its not seems working.我尝试使用 isSubmitting 属性更改按钮启用/禁用的状态,但它似乎不起作用。 Neither button text updating nor styling.按钮文本更新和样式都没有。 I have found a example solution but that's not working either.我找到了一个示例解决方案,但这也不起作用。 Here is the link of example code that isn't working.这是不起作用的示例代码的链接。 Can anyone explain please?谁能解释一下? Formik sandbox code example Formik 沙箱代码示例

You don't need to do anything special - formik automatically awaits the onSubmit handler.您不需要做任何特别的事情——formik 会自动等待onSubmit处理程序。 Just make sure it's returning a Promise that can be awaited.只要确保它返回一个可以等待的Promise即可。

In your example you are returning immediately from handleSubmit .在您的示例中,您将立即从handleSubmit返回。

 async function handleSubmit(values, { setSubmitting }) {
    console.log("submitted");

    setTimeout(() => setSubmitting(false), 2000);
    // this returns immediately
  }

Update your example code like this:像这样更新您的示例代码:

  const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms))

  async function handleSubmit(values) {
    console.log("submitted",  values);
    await delay(2000)
  }

编辑 formik-issubmitting-working(分叉)

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

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