I'm using ReactJS in frontend and NodeJS in backend and I'm trying to redirect with navigate function but I'm getting the error. What should I do exactly? Where am I missing? How can I redirect if the registration form is successful?
My action:
export const signup = (formData,navigation) => async (dispatch) => {
try {
const { data } = await API.signUp(formData)
dispatch({type:AUTH,payload:data})
let navigate = useNavigate();
navigate("/");
} catch (error) {
dispatch({type:SIGNUP_FAIL,payload:
error.response && error.response.data.message ? error.response.data.message : error.message
})
}
}
My component:
const signUpForm = (e) => {
e.preventDefault()
if(!login){
dispatch(signup(form,navigation))
}
}
You need to move the hook outside of the function:
const signUpForm = (e) => {
let navigate = useNavigate();
e.preventDefault()
if(!login){
dispatch(signup(form,navigation,navigate))
}
}
and:
export const signup = (formData,navigation,navigate) => async (dispatch) => {
try {
const { data } = await API.signUp(formData)
dispatch({type:AUTH,payload:data})
navigate("/");
} catch (error) {
dispatch({type:SIGNUP_FAIL,payload:
error.response && error.response.data.message ? error.response.data.message : error.message
})
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With