Search code examples
react-nativeasyncstorage

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?


Solution

  • 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))
    }