Home > Mobile >  this.props.history.push has undefined state
this.props.history.push has undefined state

Time:05-10

I am trying to send some data from my SignInForm class to another component through this.history.push using the state parameter.

However, when I look over in the AdminPage class, the props for state are undefined.

How can I correctly send data from one class to another using history.push?

SignInForm.js

  .then(response =>{
  const my_user = this.state.user;

  if(response.status === 200){
      this.setState({user : response.data})
    }
      if(this.state.user.roles[0].roleCode === 'ADMIN'){
        this.props.history.location.pathname = "/"; 
        
        // user :{"id":1,"name":"name ","email":"[email protected]","roles":[{"id":1,"roleCode":"ADMIN","roleDescription":"restaurant administrattor","authority":"ADMIN"}]}
        console.log("user :"   JSON.stringify(my_user));
        this.props.history.push({pathname: "adminPage", state : { user : my_user}});
      }
      else if(this.state.user.roles[0].roleCode === 'USER'){
        this.props.history.location.pathname = "/";
        this.props.history.push({pathname: "customerPage",state :{ user : my_user}});
      }

  })
  .catch(error => this.setState({error : true,message : "cannot decrypt user info"}))


  })
  .catch(error => this.setState({error : true,message : "cannot decrypt user info"}))

AdminPage.js

constructor(props){ 
    super(props)
    this.state = {
        user : this.props.history.location.state.user
    }
    console.log(this.state.user);
}

CodePudding user response:

this.props.history.location.pathname = "/";

instead

this.props.history.push("/");

use this

CodePudding user response:

    this.props.history.push({pathname: "adminPage", state : { user : my_user}});

change into

 this.props.history.push("/adminPage", { state: user}); 

Refrence 1 Refrence 2

  • Related