提交值后,如何在React挂钩中使用回调函数?

时间:2020-03-12 08:14:15

标签: reactjs react-hooks

我正在使用React挂钩构建应用程序。

但是问题是我可以故意单击两次按钮以发生错误。

所以我想在单击按钮时使用useState发送api请求时禁用该按钮。

尽管发送api和useState都异步工作,但可能很难期望它如何工作。

因此,我想到了在React中使用setState的回调函数的想法,但是由于我是React钩子的新手,因此很难理解如何在React钩子中使用setState回调函数的想法。

代码如下:

    const submitForm = (values: IAgreeMentValues) => {
      const userActivate = Object.assign(props.currentUserObject, values, {c})
      delete userActivate.email
      const userActivateJSON = JSON.stringify(userActivate)

      if(values.is14YearsOldOrOlder && values.isAcceptedPrivacyPolicy && values.isAcceptedTerm) {

        setButtonState(true)

        activateUser(userActivateJSON).then(response => {
          if(response.data.result.canLogin) {
            popupStore.add({
              type: 'alert',
              title: t('signUp.agreeMent.registeredCompleted'),
              actionString: t('popUpAlert.ok'),
              onSubmit: isMobile ? pushToAppStore : pushToLogin
            })
          } else {
            popupStore.add({
              type: 'alert',
              title: t('error.occured'),
              actionString: t('popUpAlert.ok')
            })
          }
        })
        .catch(response => {
          setButtonState(false)
          popupStore.add({
            type: 'alert',
            title: response.response.data.error.message,
            actionString: t('popUpAlert.ok'),
          })
        })
      } else {
        popupStore.add({
          type: 'alert',
          title: t('signUp.agreeMent.mustBeChecked'),
          actionString: t('popUpAlert.ok')
        })
      }
    };


    return (
      <>
      <div className='signup-step-wrap'>
        <div className='agree-check-box'>
        <Formik enableReinitialize={true} initialValues={termsAllSelected}
          onSubmit={ (values) => submitForm(values)}>
            {({ values, handleChange, handleSubmit }) => (
              <form onSubmit={handleSubmit}>
              <ul className='check'>
              <li>
                <Field type='checkbox' name={`isAcceptedMarketingEmail`} id={`agree4`} onChange={handleChange} />
                <label htmlFor={`agree4`}>{t('signUp.agreeMent.isAcceptedMarketingEmail')}</label>
              </li>
              <li>
                <Field type='checkbox' name={`isAcceptedMarketingSms`} id={`agree5`} onChange={handleChange} />
                <label htmlFor={`agree5`}>{t('signUp.agreeMent.isAcceptedMarketingSms')}</label>
              </li>
              </ul>
              <div className='btn-btm-signin'>
                <button className='btn-signin active' type='submit' disabled={currentButtonState} style={currentButtonState ? {background: '#e2e2e2'}:{background: '#1f1f1f'}}>{t('signUp.agreeMent.next')}</button>
              </div>
              </form>
            )}
        </Formik>
        </div>
      </div>
      </>
    )

1 个答案:

答案 0 :(得分:0)

您还应该在setButtonState(false)中的then

activateUser(userActivateJSON).then(response => {
          if(response.data.result.canLogin) {
            popupStore.add({
              type: 'alert',
              title: t('signUp.agreeMent.registeredCompleted'),
              actionString: t('popUpAlert.ok'),
              onSubmit: isMobile ? pushToAppStore : pushToLogin
            })
          } else {
            popupStore.add({
              type: 'alert',
              title: t('error.occured'),
              actionString: t('popUpAlert.ok')
            })
          }
          setButtonState(false)
        })