How to store value in local storage in React Native?

Sometimes, we want to store value in local storage in React Native.

In this article, we’ll look at how to store value in local storage in React Native.

How to store value in local storage in React Native?

To store value in local storage in React Native, we can use AsyncStorage.

For instance, we write:

import * as React from 'react';
import { AsyncStorage, View } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';

export default function App() {
  const getEntries = async () => {
    await AsyncStorage.setItem('key', 'val');
    const value = await AsyncStorage.getItem('key');
    console.log(value);
  };

  React.useEffect(() => {
    getEntries();
  }, []);

  return <View></View>;
}

to call AsyncStorage.setItem with the key and value to storage the entry with the key and value.

Then we call AsyncStorage.getItem with the key to return the value with the given key.

Therefore, value is 'val'.

Conclusion

To store value in local storage in React Native, we can use AsyncStorage.