Javascript 无法访问React类组件中函数内的道具和状态

Javascript 无法访问React类组件中函数内的道具和状态,javascript,reactjs,state,react-props,react-lifecycle,Javascript,Reactjs,State,React Props,React Lifecycle,我试图从我的状态和功能中的道具中获取值。此函数在componentWillReceiveProps方法下激发。然而,当我尝试在该函数下控制台记录我的道具和状态值时,结果是“未定义”。是否与我的状态更新不够快有关?我是不是错过了传递道具的机会?哪里可以放置错误,我如何让我的函数处理正确的值 handleShow()函数: state = { training: {}, isCreator: null } componentWillReceiveProps(newProps) {

我试图从我的状态和功能中的道具中获取值。此函数在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
        })
    }
}

我不知道y道具令牌不工作可能组件未重新呈现检查u接收道具控制台。Log(this.props)和in componentWillReceiveProps函数handleshow您可以通过这种方式传递newprops令牌您可以使用令牌检查条件和状态handleshow(newprops.token)。最后,检查父组件的属性可能您可以尝试改用“componentDidUpdate”。
componentWillReceiveProps
不安全(在React v17中不起作用)。您确定该组件收到了一些道具吗?是的,当我在axios.get下控制台记录'res.data'时,我得到了正确的结果。这意味着我的道具是正确的,但不知何故我无法在我的函数下访问它们。@marzelin,这很奇怪,因为当我重新加载页面时,道具在函数中是正确的