I have an issue, this issue is to take an error from my try catch and pass this message error to my custom alert component.
Below is my code:
export const Register = () => {
const dispatch = useDispatch();
const [openError, setOpenError] = useState(false);
const AlertError = openError ? (
<Dialog open={openError}>
<AlertCard
open={openError}
handleClose={handleCloseError}
borderTop="1vh solid red"
color="black"
severity="error"
title="Cadastro falhou"
description='message' //put here message error
/>
</Dialog>
) : (
""
);
const formik = useFormik({
initialValues: {
email: "",
password: "",
first_name: "",
last_name: "",
confirmPassword: "",
newsletter: "",
},
validationSchema: validationSchema,
onSubmit: async (values) => {
setIsLoading(true)
dispatch(signupUserPending());
dispatch(requestConfirmEmailPending());
try {
if (document.getElementById("checkbox").checked) {
values.newsletter = "on";
}
const singUpRes = await signup(values);
dispatch(signupUserSuccess(singUpRes));
dispatch(getUserSuccess(singUpRes));
dispatch(getTokenSuccess(singUpRes));
const confirmEmailRes = await requestConfirmEmail(singUpRes.user.email);
dispatch(requestConfirmEmailSuccess(confirmEmailRes));
setOpenSuccess(true);
navigate("/confirm-token");
} catch (err) {
console.log(err.message); //need take this message error
dispatch(signupUserFailure(err));
dispatch(requestConfirmEmailFailure(err));
setIsLoading(false);
setOpenError(true);
}
},
});
return ({AlertError}...rest of code)
}
I need to catch a message error which come from the backend, and put this error inside my props description on my custom alert error. But I don't know how to make this. I tried make this for some hours but could not achieve success.
Via Active questions tagged javascript - Stack Overflow https://ift.tt/iBCHoGU
Comments
Post a Comment