2016-10-04 90 views
4

我有一个注册表单,用户注册后,它将重定向到用户输入通过电子邮件发送的确认码的电子邮件确认页面(/确认)。当用户提交确认码时,我想将代码和用户的电子邮件发送到服务器端。如何在React中重定向到其他路线时传递状态/道具?

我的申请表单的代码(简化):

constructor(props){ 
      super(props); 
      this.state = { 
      email: '', 
      password: '', 
      errors: {}, 
      } 
     } 
    handleSubmit(e){ 
     e.preventDefault(); 
     this.props.userSignup(this.state).then(
      () => { 
       this.context.router.push({ 
        pathname: '/confirmation' 
       }) 
      }, 

     ) 
    } 
    render(){ ... 

我不想任何PARAMS添加到我的路径。
如何将this.state.email传递给确认页?

+0

使用反应路由器https://github.com/ReactTraining/react-router –

回答

10

我想通了。

this.context.router.push({ 
    pathname: '/confirmation', 
    state: {email: this.state.email} 
}) 

和访问状态是:

this.props.location.state.email 
+0

非常感谢你张贴,也有同样的问题,你的答案 –

+0

@ Full-Hybrid没问题! – Dan

+0

值得注意的是,上下文并不稳定或在React中推荐。 –

相关问题