简体   繁体   中英

this.props.history.push has undefined state

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":"name@gmail.com","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);
}
    this.props.history.push({pathname: "adminPage", state : { user : my_user}});

change into

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

Refrence 1 Refrence 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM