小编典典

在React中重定向到另一个路由时如何传递状态/属性?

reactjs

我有一个注册表单,用户注册后,它将重定向到电子邮件确认页面(/
confirmation),用户在其中键入我通过电子邮件发送的确认码。当用户提交确认码时,我想将验证码和用户的电子邮件发送到服务器端。

我的注册表单代码(简体):

    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(){ ...

我不想在路径中添加任何参数。
如何this.state.email进入确认页面?


阅读 285

收藏
2020-07-22

共1个答案

小编典典

我想到了。

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

并通过以下方式访问状态:

  this.props.location.state.email
2020-07-22