react router - Redirection after login

后端 未结 7 2185
天命终不由人
天命终不由人 2021-01-30 14:22

Could you please help me in understanding the redirection mechanism I could use with latest version of react router ( v1.1.0 ) . I would like to redirect to a

7条回答
  •  有刺的猬
    2021-01-30 14:35

    As @JohnSz mentions I too had issues with using withRouter. Instead I did it as instructed here: https://github.com/reactjs/react-router/blob/master/upgrade-guides/v2.0.0.md#programmatic-navigation

    const RouteComponent = React.createClass({
      contextTypes: {
        router: React.PropTypes.object.isRequired
      },
      someHandler() {
        this.context.router.push(...)
      }
    })
    

    Basically:

    1. Define contextType
    2. Use this.context.router.push(...)

    Cheers.

提交回复
热议问题