how to access history object in new React Router v4

前端 未结 3 513
抹茶落季
抹茶落季 2020-12-29 09:07

I have tried all the suggested ways in other threads and it is still not working which is why I am posting the question.

So I have history.js looking li

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-29 09:45

    Looks to me like you might be missing a withRouter connection. this would make the history props available to this component

    ...
    
    import { withRouter } from 'react-router'
    
    class Home extends Component {
      functionMethod() {
        const { history: { push } } = this.props;
        doStuff();
        push('/location');
      }
      ...
    }
    
    export default withRouter(Home);
    

    https://reacttraining.com/react-router/web/api/withRouter

提交回复
热议问题