How to transfer state / props when redirecting to another route in React?

I have a registration form and after the user registers, he will redirect the email confirmation page (/ confirmation), where the user enters the verification code that I sent by email. When the user sends a confirmation code, I want to send the code and email address of the user to the server.

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

I do not want to add any parameters to my path.
How to transfer this.state.emailto the confirmation page?

+4
source share
1 answer

I get it.

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

and access status:

  this.props.location.state.email
+12
source

Source: https://habr.com/ru/post/1656667/


All Articles