0

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);
}
DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127
Rabi
  • 15
  • 1
  • 3

1 Answers1

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

change into

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

Refrence 1 Refrence 2

Blind2k
  • 350
  • 3
  • 13