如何解决无法在React类Component中的函数中访问道具和状态
我正在尝试从状态和道具中获取值。该函数在componentWillReceiveProps方法下触发。但是,当我尝试通过该功能控制台记录我的道具和状态值时,结果为“未定义”。与我的状态更新速度不够快有关吗?我是否想在某个地方传递道具?哪里有错误,我该如何处理正确的值?
handleShow()函数:
state = {
training: {},isCreator: null
}
componentWillReceiveProps(newProps) {
console.log(newProps);
if (newProps.token) {
axios.defaults.headers = {
"Content-Type": "application/json",Authorization: newProps.token
}
const trainingID = this.props.match.params.trainingID;
axios.get(`http://127.0.0.1:8000/api/${trainingID}/`)
.then(res => {
this.setState({
training: res.data
});
console.log('treener',res.data.coach)
})
.catch(err => {console.log(err)})
this.handleShow()
} else{}
}
handleShow() {
if(this.props.token == this.state.training.coach) {
console.log('Props:',this.props.token);// value of undefined
console.log('State.training',this.state.training.coach)//
this.setState({
isCreator: true
})
}else{
this.setState({
isCreator: null
})
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。