this.props.history.push works in some components and not others

后端 未结 5 1744
南旧
南旧 2020-12-25 10:58

I am trying to programmatically change pages using browserHistory.push. In one of my components, but not in a component that I embedded inside of that one.

5条回答
  •  情深已故
    2020-12-25 11:48

    You Can try this

    this.context.history.push('/dashboard')
    

    or pass the history as a prop from parent to child component like

    parent

    
    

    child

    this.props.history.push('/dashboard');
    

    or withRouter

    import { withRouter } from "react-router";
    export default withRouter(connect(mapStateToProps, {
        ...
    })(Header));
    

提交回复
热议问题