react-router go back a page how do you configure history?

前端 未结 21 1158
说谎
说谎 2020-11-30 18:30

Can anyone please tell me how I can go back to the previous page rather than a specific route?

When using this code:

var BackButton = React.createCla         


        
相关标签:
21条回答
  • 2020-11-30 19:04

    This piece of code will do the trick for you.

    this.context.router.history.goBack()
    
    0 讨论(0)
  • 2020-11-30 19:06

    Go back to specific page:

      import { useHistory } from "react-router-dom";
    
      const history = useHistory();
      
      const routeChange = () => {
        let path = '/login';
        history.push(path);
      };
    

    Go back to previous page:

      import { useHistory } from "react-router-dom";
    
      const history = useHistory();
      
      const routeChange = () => {
        history.goBack()
      };
    
    0 讨论(0)
  • 2020-11-30 19:06

    The only solution that worked for me was the most simple. No additional imports needed.

    <a href="#" onClick={() => this.props.history.goBack()}>Back</a>
    

    Tks, IamMHussain

    0 讨论(0)
提交回复
热议问题