Saving data on the phone
You'll learn to save data on the phone with AsyncStorage, so your habits are still there after the app closes.
Watch & read
Start with the required ones. The others are there when you need them.
Explanation
State lives in the phone's memory only while the app is open. Close the app, and every ticked habit is gone. You need to write it down somewhere that stays.
Think of a notebook with labelled pages. You write on the page called "habits", and next time you open the notebook to the same page and read it back. AsyncStorage is that notebook, kept on the phone. Each page has a name (the key) and holds one string (the value).
import AsyncStorage from "@react-native-async-storage/async-storage";await AsyncStorage.setItem("habits", JSON.stringify(habits)); // saveconst saved = await AsyncStorage.getItem("habits"); // load: a string, or nullconst habits = saved ? JSON.parse(saved) : [];
setItem(key, value)saves a string. Your habits are an array of objects, so first turn them into text withJSON.stringify.getItem(key)reads it back as a string.JSON.parseturns the text into an array again. If nothing was ever saved under that key, you getnull, so give a starting value.- Both are async, like
fetch: they return a promise, so youawaitthem.
In a screen you do two things with useEffect: load once when the screen opens ([]), and save whenever
the habits change ([habits]). Install it with npx expo install @react-native-async-storage/async-storage;
expo install picks the version that matches your Expo SDK. Snack adds it for you when you import it.
People often save the array without JSON.stringify. AsyncStorage only stores strings. Stringify on
the way in, parse on the way out, every time. The other trap: the save effect also runs on the very first
render, while habits is still [], and can write an empty list over your saved one before loading is
done. Keep a loaded flag and only save after loading.
AsyncStorage is for small things: a list of habits, a setting, a name. It is not encrypted, so never keep
passwords in it. For a lot of data that you search and sort, Expo has expo-sqlite, a real database on
the phone.
When you read storage code, find the key string (it must be the same for saving and loading), then check
for stringify before saving and parse after loading.
Code tour
Click a line number with a ring to read the note for that line.
Practice
Short drills. Take your time and read the code before you answer.
1async function loadHabits() {2 const saved = await AsyncStorage.getItem("habits");3 console.log(saved);4}56// The app was just installed. Nothing has ever been saved.7loadHabits();
1import AsyncStorage from "@react-native-async-storage/async-storage";2import { useEffect, useState } from "react";3import { FlatList, Pressable, StyleSheet, Text, View } from "react-native";45const start = [6 { id: "h1", name: "Drink water", done: false },
Runs in the browser. Choose “My Device” in Snack to try it on your phone with Expo Go.
AsyncStorage.removeItem("habits") and sets the habits back to start.Quick check
3 questions. Each one explains itself after you answer.
- 1 / 3What kind of value does AsyncStorage keep under a key?
- 2 / 3Where should loading the saved habits go in a screen?
- 3 / 3You tick a habit, save with
setItem, and close the app completely. What happens when you open it again?