How To Easily Persist Your Data With Context and AsyncStorage

How To Easily Persist Your Data With Context and AsyncStorage

It’s a fairly common practice to persist user data between sessions in most mobile apps and to get this done with React Native is as simple as using AsyncStorage.

It’s a fairly common practice to persist user data between sessions in most mobile apps and to get this done with React Native is as simple as using AsyncStorage. This will allow you to keep changes, or updates to any components even if the app is restarted, refreshed or closed.

Take this counter for example, if you increment or decrement the count and restart the app, the count will reset back to zero.

import React, { useContext, createContext, useState } from 'react';
import { View, Button, StyleSheet, Text } from 'react-native';

const CounterContext = createContext(0);
const useCounter = () => useContext(CounterContext);
const CounterContextProvider = ({ children }) => {
  const [count, setCount] = useState(0);
  const increment = () => setCount((value) => value + 1);
  const decrement = () => setCount((value) => value - 1);
return (
    <CounterContext.Provider 
      value={{ 
        count, 
        increment, 
        decrement
       }}>
         {children}
    </CounterContext.Provider>
  );
};
const App = () => {
  const { count, increment, decrement } = useCounter();
  return (
    <View 
      style={styles.container}>
      <Text>{count}</Text>
      <Button title="Increment" onPress={() => increment()} />
      <Button title="Decrement" onPress={() => decrement()} />
    </View>
  );
};
const styles = StyleSheet.create({   
  container: {     
    flex: 1,     
    alignItems: 'center',     
    justifyContent: 'center',   
  }
});
export default () => (
  <CounterContextProvider>
    <App />
  </CounterContextProvider>
);

javascript hooks react-native persistence react

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

Hire Dedicated React Native Developer

Have you ever thought of having your own app that runs smoothly over multiple platforms? React Native is an open-source cross-platform mobile application framework which is a great option to create mobile apps for both Android and iOS. **[Hire...

React Native Mobile App Development

Skenix is providing React Native Development Services with qualified React Native App Developers. Get the best React Native App Development Services.

The Ugly Side of React Hooks

In this post, I will share my own point of view about React Hooks, and as the title of this post implies, I am not a big fan.

React Native Redux | How To Use Redux In React Native

React Native Redux Example Tutorial From Scratch. We connect the React Native app to Redux store to save and retrieve the application data.