React Native Get Save Value Locally using AsyncStorage in App Memory Example Tutorial

Storing value locally is very important for react native without using Local Database because database has its lot’s of configuration and it would make the work lengthy if we have to save few values. React Native has its own solution for this by giving us a proper fully ready component named as AsyncStorage, AsyncStorage is used to store value locally in both Android and iOS react native applications by providing us a Key factor. Developers can retrieve saved value from anywhere or any activity in complete application using the Unique Key. So in this tutorial we would going to Get Save Value Locally using AsyncStorage in App Cache Memory in iOS Android React Native App Example Tutorial.

Note: The stored value is permanent and can be accessible after closing the app or restarting the mobile until its been deleted.

Procedure covered in this App:

  • Retrieve value from TextInput and saved in App using AsyncStorage.
  • Showing saved value in Text component.

Contents in this project Get Save Value Locally using AsyncStorage in App Memory in Android iOS React Native App Example Tutorial:

1. Import StyleSheet, View, AsyncStorage, TextInput, Alert, Text and TouchableOpacity component in your project.

2. Create constructor() in your project and make 2 State named as textInputData and getValue.

textInputData : This state is used to store TextInput entered value.

getValue : This state is used to store AsyncStorage stored value inside it to display on screen.

3. Create a function named as setValueLocally() in your class. Inside this function we would store the State value with a Unique key created by myself Key_27, Now it means this key would contain the State value inside and we can access this value from any where in the app using this key, You can set your key name its upon you.

4. Create a function named as getValueLocally(), Inside this function we would retrieve the Stored value using our Key_27 key and again store the value in getValue State. We would use this state to show the value on screen using Text component.

5. Creating 1 TextInput, 2 Buttons using TouchableOpacity and 1 Text component inside render’s return block.

TextInput : Used to retrieve value from application user.

TouchableOpacity : Creating 2 Buttons to call the above both functions.

Text : Used to display the Stored value.

6. Creating Style.

7. Complete source code for App.js File :

Screenshots:


Get Save Value Locally using AsyncStorage