Hướng dẫn dùng asyncstorage JavaScript
5 is a simple, asynchronous, unencrypted by default module that allows you to persist data offline in React Native apps. The persistence of data is done in a key-value storage system. Show There are numerous scenarios where this module can be beneficial. Persisting data in a mobile app has benefits such as when the user restarts the app, the data or the setting variables being available to the user in the state they left before closing the app. This scenario is going to be covered in this tutorial. Find the complete code for this tutorial at this GitHub repo. Installing the community moduleReact Native version 6 has lots of its API modules available as separate packages that you can easily install with a package manager such as npm or yarn. These packages are managed by community members as well as open-source developers.To begin, let us create a new React Native project and install the module: @react-native-async-storage/async-storage. From a terminal window, execute the following commands in the order they are specified.
If you are using the latest React Native version ( 7 or above) you do not have to link the native binaries for iOS and Android platforms manually. This linking of the module is handled automatically by the CLI. Though, for iOS, you have to install cocoapods. Go back to the terminal window and execute the following command:
That's it in terms of configuring the AsyncStorage module to work in the React Native app. Changing the default storage limitation on AndroidOn iOS devices, the AsyncStorage is not limited programmatically. On Android devices, by default, the current AsyncStorage size is set to 8. Reaching this limit is going to result in errors like the database or disk being full. There are valid reasons why this limit exists but if there is a need to increase the size, you can do that. Open the 9 file and add the following:
This file represents any project-wise settings that you can use to override the default values or settings. Now, to make sure everything is working correctly, build the Android app (and make sure an Android device or emulator is running before you execute the command below).
You are going to get the default screen that confirms that the app is configured. Similarly, to build the app for iOS, execute the command below:
Using the AsyncStorage APILet's start creating an example. In this example application, you will save a value from the user's input in the storage and fetch the value from the storage. This reading and writing of data are going to be done with the help of AsyncStorage API functions. Open the 0 file and start by importing the following components.
Next, define a variable name 1. It is used to read and save the data. You can think of this key as the identifier to the value that is being stored, hence, the key-value storage system. As you are going to store only one value at the moment, there is only one key required.
Inside the functional component 2, define a state variable 3 that has an empty string as its default value.
Saving the dataTo save the data for the app to read, let us define an asynchronous helper method called 4. This method is going to be promise-based, which means you can use the 5 syntax with a 6 block.Passing the identifier 1 as well as the 3 state variable, the API method 9 is going to store the value in storage.Let us update the state as well as display an alert box when the data is successfully stored.
Reading the dataWhen the app restarts, it should be able to read the data that persisted in the previous section. For this, create another helper function called 0 that is going to be asynchronous. This function is going to read the value from the storage using the API method 1 and will only set the value of the state variable input when its value is not 2.
To retrieve the data whenever the app starts, invoke this method inside the 3 hook. 0Clearing all storageAnother API method that the current demo app is going to utilize from the AsyncStorage API is called 4. This method deletes everything that was previously saved.If you want to delete only a specific item from the storage, there are other methods available such as 5 or 6 by the AsyncStorage API.Add the following snippet: 1Controlling the inputThe last two helper functions this example app requires are 7 and 8.These methods are going to be responsible for reading the input and updating the state variable as well as storing the input. 2Completing the appWe have defined all the helper functions that are required by the app. Let us complete the 9 statement by defining the JSX as below: 3Here are the corresponding styles to the above code snippet: 4Now, go back to the iOS simulator and you are going to be welcomed by the following screen: Next, enter a value, close the app, and then open the app to see if the value was stored or not. Lastly, see what happens when you click on the "Clear Storage" button. ConclusionCongratulations! I hope that, in this tutorial, you learned the basic API methods of using AsyncStorage API in your React Native app. Even though the demo app was minimal, it allowed us to understand the concepts. Here is the link to the complete API for the 5 module that you can refer to for further reading.And speaking of further reading, don't miss our guide on React Native Security and our tutorial on protecting the source code of React Native apps against code tampering and reverse-engineering. |