Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

yup - Formik setStatus not updating - formik.status undefined

I am trying to manually set the status on my formik instance upon receiving an error response from server:

  const formik = useFormik({
    initialValues: {
      email: "",
      password: "",
    },
    onSubmit: async ({ email, password }, { setStatus }) => {
      try {
        const response = await loginUser({
          variables: {
            email,
            password,
          },
        });
      } catch (error) {
        await setStatus({ email: error.message, password: error.message });

        console.log(formik.status);
      }

      formik.resetForm();
    },
    validationSchema: Yup.object().shape({
      email: Yup.string(),
      password: Yup.string(),
    }),
  });

Logging status prints undefined.

Why is status not being set?

question from:https://stackoverflow.com/questions/65895856/formik-setstatus-not-updating-formik-status-undefined

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Figured it out.

Using resetForm clears both form status and errors.

To get around this I have manually cleared both fields instead:

  initialValues={{
    email: "",
    password: "",
  }}
  onSubmit={async ({ email, password }, { setStatus, setFieldValue }) => {
    try {
      const response = await loginUser({
        variables: {
          email,
          password,
        },
      });
    } catch (error) {
      setStatus({
        email: "Shit",
      });

      setFieldValue("email", "");
      setFieldValue("password", "");
    }
  }}
  validationSchema={Yup.object().shape({
    email: Yup.string(),
    password: Yup.string(),
  })}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...