【发布时间】:2020-12-15 11:24:57
【问题描述】:
为什么我在this.props 和this.state 下的值在我的componentDidMount 方法中未定义?在我的类组件的其他部分,我可以正确访问我的道具和状态值。我是否需要将它们单独传递到某个地方或我在哪里犯了任何错误?
我没有得到任何值:
componentDidMount() {
console.log(this.props.token)
console.log(this.state.training.coach)
// values are null and undefined.
if (this.props.token == this.state.training.coach) {
this.setState({
iscreator: true
});
console.log(this.state.iscreator)
} else {
this.setState({
iscreator: false
});
}
}
访问this.props.token 时我得到正确的值:
handleDelete = (event) => {
if (this.props.token !== null) {
const trainingID = this.props.match.params.trainingID;
axios.defaults.headers = {
"Content-Type": "application/json",
Authorization: this.props.token
}
axios.delete(`http://127.0.0.1:8000/api/${trainingID}/`)
this.props.history.push('/');
this.forceUpdate();
} else {
}
}
【问题讨论】:
标签: javascript reactjs components react-props react-state