How to push to History in React Router v4?

后端 未结 21 1856
不思量自难忘°
不思量自难忘° 2020-11-22 00:27

In the current version of React Router (v3) I can accept a server response and use browserHistory.push to go to the appropriate response page. However, this isn

21条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 01:25

    Now with react-router v5 you can use the useHistory lifecycle hook like this:

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

    read more at: https://reacttraining.com/react-router/web/api/Hooks/usehistory

提交回复
热议问题