如何在React中重定向到另一条路径时传递状态/道具?

Dan*_*Dan 7 reactjs react-router

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

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

    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(){ ...
Run Code Online (Sandbox Code Playgroud)

我不想在我的路径上添加任何参数.
我怎样才能this.state.email转到确认页面?

Dan*_*Dan 25

我想到了.

 this.context.router.push({
     pathname: '/confirmation',
     state: {email: this.state.email}  
 })
Run Code Online (Sandbox Code Playgroud)

和访问状态:

  this.props.location.state.email
Run Code Online (Sandbox Code Playgroud)


Roh*_*ali 5

我在没有上下文对象的情况下传递数据的方法是使用历史记录道具,

this.props.history.push({
             pathname:"/shopdetail",
             state:{
                 key:"value"
              }
            });
Run Code Online (Sandbox Code Playgroud)

在ShopDetail组件中,您可以访问以下对象:

this.props.location.state.key
Run Code Online (Sandbox Code Playgroud)