State Management and Data Persistence
Lesson opener
In one line: UseState for in-memory UI state (same as React web).
Why it matters: This shows up at work when you need to useState for in-memory UI state (same as React web).
Before this lesson: Navigation
Before you start
| Resource | Link |
|---|---|
| Previous | Navigation |
| Next | Number guessing game capstone |
| Time | ~45 minutes |
What you will learn
useStatefor in-memory UI state (same as React web)AsyncStoragefor data that survives app restarts- When to use each — and the offline expectation on mobile
Section 1 — Web vs mobile data expectations
Plain English:
In a website, data can live on a server and be fetched on each page load. In a mobile app, users expect data to work offline. Your app must store data locally and sync when connected (sync comes later — local first).
| Storage | Web | React Native |
|---|---|---|
| Session UI state | useState | useState — same |
| Persist after close | localStorage (sync) | AsyncStorage (async) |
| Server data | fetch() | fetch() — same |
Section 2 — useState (unchanged from React)
import { useState } from 'react';
import { View, Text, Button } from 'react-native';
export default function Counter() {
const [count, setCount] = useState(0);
return (
<View>
<Text>Count: {count}</Text>
<Button title="Add one" onPress={() => setCount(count + 1)} />
</View>
);
}When count resets: app reload in dev, or OS kills the process. Not when navigating between screens (component stays mounted) unless you unmount it.
Section 3 — AsyncStorage
Install:
npx expo install @react-native-async-storage/async-storageimport AsyncStorage from '@react-native-async-storage/async-storage';
// Save data (must be a string)
await AsyncStorage.setItem('user_score', JSON.stringify(score));
// Load data
const saved = await AsyncStorage.getItem('user_score');
const score = saved ? JSON.parse(saved) : 0;
// Remove
await AsyncStorage.removeItem('user_score');Breaking it down:
- Async — always use
awaitor.then(); never assume instant read - Strings only — use
JSON.stringify/JSON.parsefor objects and numbers - Keys — pick stable names like
'high_score'not'temp'
Use this when: high scores, user preferences, draft form text before lifecycle pause.
Section 4 — Load on mount pattern
import { useState, useEffect } from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';
const KEY = 'user_score';
export default function ScoreDisplay() {
const [score, setScore] = useState(null);
useEffect(() => {
(async () => {
const saved = await AsyncStorage.getItem(KEY);
setScore(saved ? JSON.parse(saved) : 0);
})();
}, []);
async function saveScore(newScore) {
setScore(newScore);
await AsyncStorage.setItem(KEY, JSON.stringify(newScore));
}
if (score === null) {
return <Text>Loading...</Text>;
}
return <Text>Score: {score}</Text>;
}Lifecycle link: When the user switches apps (android1), in-memory
useStatemay be lost if the OS destroys your process. AsyncStorage survives — that is why the capstone game persists high scores.
Practice
Do this now: Build a counter that persists across app restarts using AsyncStorage. Force-close Expo Go and reopen — count should remain.
Checklist
- [ ] Can you explain when to use
useStatevs AsyncStorage? - [ ] Can you save and load JSON with
await? - [ ] Do you know why AsyncStorage is async?
What's next
Number guessing game — capstone using state, storage, input, and navigation together.