Hello Friends đź‘‹,
Welcome To Infinitbility! ❤️
React Native Asyncstorage - setItem() |
React Native Asyncstorage - getItem() |
React Native Asyncstorage - removeItem() |
React native asyncstorage provide getItem()
method to get stored value from asyncstorage, it will return string
value and if you stored number and object then you to convert it first from string.
In this tutorial, we will learn to get value from asyncstorage with example of diffrent datatypes like string, number, or object.
let start today’s tutorial How to get value from asyncstorage in react native?
React Native asyncstorage getItem
Async Storage can only return string
data, so in order to use number
or object
we have to first converts it from string
.
To get value from asyncstorage, React native asyncstorage provide getItem()
method, it will expect storage key
and return value
.
Check below examples to get string, number, or object in AsyncStorage.
- Get string example
import AsyncStorage from '@react-native-async-storage/async-storage';
const getData = async () => {
await AsyncStorage.getItem('@storage_Key');
}
- Get number example
To get number in AsyncStorage we have to first convert it to number using parseInt()
method.
import AsyncStorage from '@react-native-async-storage/async-storage';
const storeData = async () => {
let age = await AsyncStorage.getItem('@age');
if(age){
age = parseInt(age);
}
}
- Get object example
To get object in AsyncStorage we have to first convert to object using JSON.parse()
method.
import AsyncStorage from '@react-native-async-storage/async-storage';
const storeData = async () => {
let user = await AsyncStorage.setItem('@user');
if(user){
user = JSON.parse(user);
}
}
Thanks for reading…
if you get stuck, reach me at infinitbility@gmail.com