How to write a value to state?

How to write the value in didMount from async storage in the state?
constructor(props) {
super(props);
 this.state = {
 access_token: ",
};
}

 componentDidMount() {
 AsyncStorage.getItem('access_token').then(value => {
 this.setState({access_token: value});
});

console.log(this.state.access_token);
 }
April 3rd 20 at 17:31
2 answers
April 3rd 20 at 17:33
Apparently, You have a gap in the asynchronous model JS.
In Your case, happens about the following:
--- internal tick
- componentDidMount
- asyncStorage.getItem
- console.log // at this point we have not got in then
--- internal tick
- then the unit
- setState


To read the token, you must either:
- to pass the callback as the second argument to setState, which will be called after state is updated, and call console.log
- call consoe.log in componentDidUpdate
April 3rd 20 at 17:35
The answer to your question: you do it, all right.
The answer to the question "how to read the value of state after recording when didMount" gave @antonietta.Jerde.:)

Find more questions by tags JavaScriptReactReact Native