重定向到 React 中的另一个路由时如何传递状态/道具? [英] How to pass state/props when redirecting to another route in React?

查看:50
本文介绍了重定向到 React 中的另一个路由时如何传递状态/道具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a signup form and after a user signs up, it will redirect to the email confirmation page(/confirmation) where a user types a confirmation code that I sent via an email. When a user submits a confirmation code, I want to send the code and a user's email to server-side.

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

My signup form code(simplified):

    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 传递到确认页面?

I do not want to add any params to my path.
How can I pass this.state.email to confirmation page?

推荐答案

我想通了.

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

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

and access state by:

  this.props.location.state.email

这篇关于重定向到 React 中的另一个路由时如何传递状态/道具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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