首页 > 解决方案 > formik onChange 不能异步工作

问题描述

  name: Yup.string()
    .required(),
});

  const {
    errors,
    touched,
    getFieldProps,
    handleChange,
  } = formik;


  function onChange(e) {
    if (Object.keys(errors).length === 0) {
      **(HERE I HAVE errors.length 0, despite this my input is empty and is required, so I shuld have errors here. I have error here, but with delay, )**
      dispatch(setInvitationConfigParam({ name: e.target.name, value: e.target.value }));
    }
    handleChange(e);
  }

<InputWrapper>
  <TextInput
   customClass="input"
   error={false}
   textFieldProps={{
   label: t('configurationForm.settings.nameConfiguration'),
   error: hasErrors('name', errors, touched),
   helperText: getErrors('name', errors, touched),
   ...getFieldProps('name'),
   onChange,
}}
tooltipTitle={t('configurationForm.settings.nameConfigurationDescription')}
/>

我想要什么以及为什么我有问题。

每次用户更改他的输入名称时,我都想发送调度函数,但是如果输入名称为空,我不想发送调度。

问题是:对象errors显示错误有延迟。我怎么能errors没有交易?

标签: reactjsvalidationasynchronousonchangeformik

解决方案


推荐阅读