用户在Formik字段中选择一个选项时,是否可以设置状态?

时间:2020-06-29 11:57:12

标签: javascript reactjs formik

我在Formik表单中有一个字段:

<br> followed by <INLINE ELEMENT> = Next line starting with <INLINE ELEMENT>
<br> followed by <BLOCK ELEMENT> = Ignore since <BLOCK ELEMENT> will render on the next line anyway

此字段的值更改时,我正在使用js onchange设置状态:

                        <Field
                          name="country"
                          as="select"
                          className={
                            "form-control" +
                            (errors.country && touched.country
                              ? " is-invalid"
                              : "")
                          }
                        >
                          <option value="" disabled selected>
                            Select Country
                          </option>
                          {countries &&
                            countries.countries.map((country) => (
                              <option id={country.id} value={country.name}>
                                {country.name}
                              </option>
                            ))}
                        </Field>
                        <ErrorMessage
                          name="country"
                          component="div"
                          className="invalid-feedback"
                        />

每当我调用setCountryid()时,该选项都不会更改(它停留在“选择国家/地区”),但是一旦我注释了该行,它就会开始工作。为什么会发生这种情况,如何设置状态并仍然选择一个选项?

0 个答案:

没有答案