I am trying to get values from my state and props in function. This function is fired under componentWillReceiveProps method. However when i try to console log my props and state values under that function i get 'undefined' as result. Is there something to do with my state not updating fast enough? Am i missing to pass props somewhere? Where could lay error and how could i get my function to handle correct values?
handleShow() function:
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
})
}
}