0

I am using redux-toolkit and I need to navigate to another page when user logs in. However I cant use the useNavigate hook because it can't be called outside any custom hook or function component. Any workaround?

export const LogUserIn = (email, password, dir) => {
  return async (dispatch) => {
    fetch(`http://localhost:3000/${dir}`, {
      method: "post",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        email: email,
        password: password,
      }),
    })
      .then((r) => {
        r.json().then((r) => console.log(r));
        dispatch(
          authSlice.actions.loginUser({ payload: { useremail: email } })
        );
      //usenavigate() will throw error 
      })
      .catch((e) => console.log(e));
  };
};

This is how I dispatch the above

const submitInfo = () => {
  dispatch(LogUserIn(statedata.email, statedata.password, "signUp"));
};
Drew Reese
  • 165,259
  • 14
  • 153
  • 181

0 Answers0