L
L
lexstile2021-12-16 19:10:11
React
lexstile, 2021-12-16 19:10:11

How to redirect via react router dom in async action?

You need to redirect the user after successful registration to the login page.
I would like to get by with the capabilities of the react router, but I am also considering other options.
Preferably without passing history (useHistory) as an argument to createUser.
package.json

{
"react-router-dom": "^5.3.0"
}

Register.jsx
const onSubmit = ({ phone, email, password }, { setSubmitting }) => {
    dispatch(createUser({ phone: replace(phone, /\D/g, ''), email, password }));
    setSubmitting(false);
  };

          <Formik
            validationSchema={RegisterSchema}
            initialValues={{ [FIELDS.PHONE]: '', [FIELDS.EMAIL]: '', [FIELDS.PASSWORD]: '' }}
            onSubmit={onSubmit}
          >
          // ..................
          </Formik>

createUser.js
const callRegister = (data) => http.post(METHODS.REGISTER, data);

export const createUser = (data) => async (dispatch) => {
  const [execute] = apiShell(callRegister, dispatch);
  const response = await execute(data);

  const success = get(response, 'success') || false;

  if (success) {
    // здесь нужен редирект
  }
};


UPD: in general, I did it while it works.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question