Automatic redirect after login with react-router

后端 未结 7 2524
广开言路
广开言路 2020-12-01 07:17

I wanted to build a Facebook login into my react/react-router/flux application. I have a listener registered on the login event and would like to redirect t

7条回答
  •  星月不相逢
    2020-12-01 07:40

    React Router v2

    Even though the question is already answered, I think it's relevant to post the solution that worked for me, since it wasn't covered in any of the solutions given here.

    First, I'm using the router context on my LoginForm component

    LoginForm.contextTypes = {
      router: React.PropTypes.object
    };
    

    After that, I can access the router object inside my LoginForm component

    handleLogin() {
      this.context.router.push('/anotherroute');
    }
    

    PS: working on React-router version 2.6.0

提交回复
热议问题