Answer the question
In order to leave comments, you need to log in
How to write a value to state?
How to write value when didMount from async storage to 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);
}
Answer the question
In order to leave comments, you need to log in
Apparently you have a gap in the asynchronous JS model.
In your case, something like this happens:
--- internal tick
- componentDidMount
- asyncStorage.getItem
- console.log // на этот момент мы ещё не попали в then
--- internal tick
- then блок
- setState
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question