如果登录正确则重定向 [英] Redirect if login is correct

查看:42
本文介绍了如果登录正确则重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 React 中,我有以下形式:

 

<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>登录</button></表单>

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

submit(e : Event) {e.preventDefault();firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password).then(function() {链接到("/");}).catch(函数(错误){控制台日志(错误)});}

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

解决方案

use react route 推送功能.路由器可以通过上下文类型传递给您的组件

一些例子怎么做

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

React.createClass({上下文类型:{路由器:React.PropTypes.object},触发器重定向(){var router = this.context.router;router.push("/wynsure/dashboard");}

In React, I have the following form:

    <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>

In case the login is correct, I would like to be redirected to the index page. So I would want something like this:

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

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

However, I know that Link.to("/") does not exist. So what can I use instead?

解决方案

use react routeur push function. The router can be pass to your component through context type

There is some examples on how to do it

Globally, all routed components received a contextType router. In a routed component (one targetted by a react-router route), you need to inform React you need the router context that's why you must declare contextTypes. it looks like:

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

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

这篇关于如果登录正确则重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆