Rajan Lagah
Rajan Lagah

Reputation: 2528

AsyncStorage in react native

componentDidMount(){

    console.log("get")
    AsyncStorage.getItem('token')
                    .then((res)=>{
                console.log(res)    
            })

}

I have componentDidMount with console.log and AsyncStorage.getItem function.I want to make axios call after getting the token value.
But the thing is AsyncStorage.getItem is not working get //console.log('get') is printed as log without res.
Any help please how to get value? am I doing something wrong?

Upvotes: 0

Views: 137

Answers (2)

Rajan Lagah
Rajan Lagah

Reputation: 2528

Try to Just restart the app and asyncStorage will behave properly.

Upvotes: 0

crystal_17
crystal_17

Reputation: 737

You can do it as follows.

componentDidMount(){
    this.onGetToken();
}
onGetToken = async () => {
    try {
      const token = await AsyncStorage.getItem('token');
      console.log("get")
      // call api using axios with token value
    } catch(e => console.info('error for getting token', e))
}

Upvotes: 2

Related Questions