Search code examples
react-nativeasyncstorage

Storing data received from an api call in react native


I have checked many links and found that AsyncStorage is the best way to store any data but it is a persistent way. I just need to know if there is any other way to store session data which can should be available for a particular session like when a user logs in to my app.

In my use case, the user logs in with credentials and then we need user specific data to be displayed anywhere we need, like other screens. Any other way apart from Asyn Storage should I be using?


Solution

  • So, if you are not using redux you can either use Asyncstorage or sqlite or realm. I would still prefer using Async storage as its persistant.

    So what your problem statement looks like is a global value which can be displayed all over the app based on user session.

    So when a user logins, you can set the Asyncstorage as

    await AsyncStorage.setItem('@storage_Key', value)
    

    and when you want to retrieve :

    const value = await AsyncStorage.getItem('@storage_Key')
    

    and suppose when the user logs out so you want to delete the async storage value as thats related with that user, you simply call the below on logout :

    await AsyncStorage.removeItem('@storage_Key')
    

    Hope it helps. feel free for doubts