Search code examples
javascriptreactjsreact-reduxmern

how to use navigate in react with react-redux


I am making a login foam on react using react-redux, kindly suggest to me how I will use navigate here

const { isAuthenticated, error, loading} = useSelector(state =>state.auth);
  useEffect(()=>{
    if(isAuthenticated){
      navigate("/dashboard");
    }
    if(error){
      // alert.error(error);
      dispatch(clearErrors());
    }
  },
  // [dispatch,alert,isAuthenticated,error,navigate]
  [dispatch,isAuthenticated,error,navigate]
  )

enter image description here


Solution

  • Did you import useNavigate at the top of the file like this?

    import { useNavigate } from "react-router-dom";

    Then, to use it, assign it to a variable:

    let navigate = useNavigate();

    then use it like you have in the code: navigate("/dashboard");