Answer the question
In order to leave comments, you need to log in
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"
}
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>
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) {
// здесь нужен редирект
}
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question