What is the best way to redirect a page using React Router?

后端 未结 5 608
刺人心
刺人心 2020-11-28 20:43

I am new to React Router and learn that there are so many ways to redirect a page:

  1. Using browserHistory.push(\"/path\")

    import          
    
    
            
5条回答
  •  -上瘾入骨i
    2020-11-28 21:00

    You can also use react router dom library useHistory;

    `
    import { useHistory } from "react-router-dom";
    
    function HomeButton() {
      let history = useHistory();
    
      function handleClick() {
        history.push("/home");
      }
    
      return (
        
      );
    }
    `
    

    https://reactrouter.com/web/api/Hooks/usehistory

提交回复
热议问题