I'm trying to log the updated values of state after setState. But they are not updating. can anyone tell me why? The updated values of states can be seen in render function but not in the tokenAccess() method.
import React, { Component } from 'react';
class Login extends Component {
constructor(props){
super(props);
this.authUrl=this.authUrl.bind(this);
this.tokenAccess=this.tokenAccess.bind(this);
this.state = {
access_token:'',
token_type:'',
expires_in:''
}
}
tokenAccess(){
const hash = window.location.hash.substring(1);
const hashArr = hash.split(/[=&]/);
console.log(hashArr);
this.setState({
access_token: hashArr[1],
token_type: hashArr[3],
expires_in: hashArr[5],
})
console.log(this.state);
}
componentDidMount(){
this.tokenAccess();
}
render() {
return (
<div>
<a href={this.authUrl()}>LOGIN</a>
<div>
{
this.state.access_token
}
</div>
</div>
)
}
}
export default Login;