简体   繁体   中英

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]
  )

在此处输入图像描述

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM