Navigate back to the homepage

How to get value from asyncstorage in react native?

Infinitbility
React Native
December 5th, 2021 · 1 min read
How to get value from asyncstorage in react native?

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
1import AsyncStorage from '@react-native-async-storage/async-storage';
2
3const getData = async () => {
4 await AsyncStorage.getItem('@storage_Key');
5}
  • Get number example

To get number in AsyncStorage we have to first convert it to number using parseInt() method.

1import AsyncStorage from '@react-native-async-storage/async-storage';
2
3const storeData = async () => {
4 let age = await AsyncStorage.getItem('@age');
5 if(age){
6 age = parseInt(age);
7 }
8}
  • Get object example

To get object in AsyncStorage we have to first convert to object using JSON.parse() method.

1import AsyncStorage from '@react-native-async-storage/async-storage';
2
3const storeData = async () => {
4 let user = await AsyncStorage.setItem('@user');
5 if(user){
6 user = JSON.parse(user);
7 }
8}

Thanks for reading…

if you get stuck, reach me at [email protected]

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo

Tutorials

Categories

More articles from Infinitbility

How to store value in asyncstorage in react native?

How to store value in asyncstorage in react native?

React Native asyncstorage setItem example

December 4th, 2021 · 1 min read
How to use await in javascript for loop?

How to use await in javascript for loop?

Example of use await in for loop in javascript, React, React Native, vue, node, deno, and all javascript framework.

December 3rd, 2021 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]