Navigate back to the homepage

How to store value in asyncstorage in react native?

Infinitbility
React Native
December 4th, 2021 · 1 min read
How to store value in 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 option to store details in application or user phones and use when you want.

In this tutorial, we will learn to store value in asyncstorage with example of diffrent datatypes like string, number, or object.

let start today’s tutorial How to store value in asyncstorage in react native?

Installation

For use asyncstorage in react native we have to first install @react-native-async-storage/async-storage package in our react native project.

  • With npm:
1npm install @react-native-async-storage/async-storage
  • With Yarn:
1yarn add @react-native-async-storage/async-storage

React Native asyncstorage setItem

Async Storage can only store string data, so in order to store number or object we have to first converts it to string.

For store value in asyncstorage, React native asyncstorage provide setItem() method, it will expect storage key and value.

Check below examples to store string, number, or object in AsyncStorage.

  • Store string example
1import AsyncStorage from '@react-native-async-storage/async-storage';
2
3const storeData = async () => {
4 await AsyncStorage.setItem('@storage_Key', "value")
5}
  • Store number example

To store number in AsyncStorage we have to first convert to string using toString() method.

1import AsyncStorage from '@react-native-async-storage/async-storage';
2
3const storeData = async () => {
4 let age = 12;
5 await AsyncStorage.setItem('@age', age.toString())
6}
  • Store object example

To store object in AsyncStorage we have to first convert to string using JSON.stringify() method.

1import AsyncStorage from '@react-native-async-storage/async-storage';
2
3const storeData = async () => {
4 let user = {
5 id: 1,
6 name: "infinitbility",
7 age: 1.4
8 };
9 await AsyncStorage.setItem('@user', JSON.stringify(user))
10}

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 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
SQLite insert or update statement example

SQLite insert or update statement example

SQLite upserts or insert or replace query example

December 2nd, 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]