Hi there! I'm a full-stack developer who loves crafting web and mobile apps. When I'm not coding, I enjoy sharing my knowledge as a technical writer. Let's build something awesome together!
When developing a React Native application, there are times when you need to store persistent and small data on the device. This means that even if you restart the application, the data still exists! Examples of such data include session/JWT values, user data, or app configurations. To achieve this, Async Storage offers a solution. It is an asynchronous, unencrypted, persistent, key-value storage system for React Native.
Async Storage in React Native has been deprecated, so the "AsyncStorage" package no longer exists within the react-native package. Instead, you need to install third-party solutions. One of the most popular libraries for using AsyncStorage in React Native is "@react-native-async-storage/async-storage". However, there are other options available as well.
Async Storage can only store string data. To store object data, you need to serialize it first. For data that can be serialized to JSON, you can use JSON.stringify() when saving the data and JSON.parse() when loading the data.
For this tutorial, we will create a basic app that reads, writes, and removes a username 🚀
The UI looks like this;
Let's implement the Async Storage business logic
Update the buttons' behavior to call methods
When the application is first opened, the load function is triggered by useEffect, so if there is data in Async Storage, it will automatically set the data!
In conclusion, Async Storage proves to be a valuable tool for React Native developers seeking a simple and efficient way to store small, persistent data on mobile devices. However, it's essential to exercise caution and avoid storing critical or sensitive data without encryption. By following these guidelines and leveraging the power of Async Storage, you can enhance your React Native applications with persistent and seamless data handling capabilities. Happy coding! 🚀