How to push to History in React Router v4?

后端 未结 21 2157
不思量自难忘°
不思量自难忘° 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:11

    If you want to use history while passing a function as a value to a Component's prop, with react-router 4 you can simply destructure the history prop in the render attribute of the Component and then use history.push()

         (
           {
              this.YourClassMethod()
              history.push('/')
            }}>
          
        )} />
    

    Note: For this to work you should wrap React Router's BrowserRouter Component around your root component (eg. which might be in index.js)

提交回复
热议问题