[英]this.props.history.push has undefined state
我正在尝试使用 state 参数通过this.history.push
将一些数据从我的SignInForm
class 发送到另一个组件。
但是,当我查看AdminPage
class 时,state 的属性是undefined
。
如何使用history.push
将数据从一个 class 正确发送到另一个?
登录表单.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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.