如果登录正确,则重定向

时间:2016-05-22 12:21:04

标签: reactjs react-router

在React中,我有以下形式:

    <div>
        <form onSubmit={this.submit.bind(this)}>
            <input type="email" name="email" placeholder="jeff@example.com" onChange={this.updateEmail.bind(this)} />
            <input type="password" name="password" placeholder="password" onChange={this.updatePassword.bind(this)} />
            <button>Login</button>
        </form>
    </div>

如果登录正确,我希望被重定向到索引页面。所以我想要这样的东西:

submit(e : Event) {
    e.preventDefault();

    firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password).then(function() {
      Link.to("/");
    }).catch(function(error) {
        console.log(error)
    });
}

但是,我知道Link.to("/")不存在。那么我可以使用什么呢?

1 个答案:

答案 0 :(得分:1)

使用react routeur push function。路由器可以通过上下文类型传递给您的组件

some examples on how to do it

在全球范围内,所有路由组件都收到了contextType router。在路由组件(一个由反应路由器路由目标)中,您需要通知React您需要路由器上下文,这就是您必须声明contextTypes的原因。它看起来像:

React.createClass({
  contextTypes: {
    router: React.PropTypes.object
  },

  triggerRedirect() {
    var router = this.context.router;
    router.push("/wynsure/dashboard");  
  }