Explain it simply
Mini Project Part 2: Todo App — State & Persistence, in plain language
We'll turn the todo array into state with useState([]) and write three functions — addTodo, toggleTodo, and deleteTodo. addTodo appends immutably with the spread operator ([...todos, newTodo]), toggleTodo uses .map() to flip just the done field on the target item, and deleteTodo uses .filter() to remove the target id. With a useEffect that auto-saves to AsyncStorage every time the todo array changes, your data will still be there each time you close and reopen the app.
Problem → Why → Solution
The problem — why this concept matters
Running the save useEffect without a loading flag, which can overwrite AsyncStorage with an empty array (before the initial load even finishes)
A common mistake
Directly mutating the array with .push()/.splice() (stick to the immutable pattern — .map()/.filter()/spread only)
Core concept — understand how it works
Evaluate Mini Project Part 2: Todo App — State & Persistence on release devices through startup, JS/UI frame rate, memory, networking, crash recovery, permissions, signing, and store requirements—not only a debug build. Recheck New Architecture and dependency compatibility on every upgrade.
- project requirements
- components, state, and services
- tested UI
Explain the React and native-platform behavior of Mini Project Part 2: Todo App — State & Persistence
Core syntax or pattern
import { useState, useEffect } from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';
function useTodos() {
const [todos, setTodos] = useState([]);
const [loaded, setLoaded] = useState(false);
useEffect(() => {
AsyncStorage.getItem('todos').then((json) => {
if (json) setTodos(JSON.parse(json));
setLoaded(true);
});
}, []);
useEffect(() => {
if (loaded) AsyncStorage.setItem('todos', JSON.stringify(todos));
}, [todos, loaded]);
const addTodo = (text) =>
setTodos([...todos, { id: Date.now().toString(), text, done: false }]);
const toggleTodo = (id) =>
setTodos(todos.map((t) => (t.id === id ? { ...t, done: !t.done } : t)));
const deleteTodo = (id) => setTodos(todos.filter((t) => t.id !== id));
return { todos, addTodo, toggleTodo, deleteTodo };
}Evaluate Mini Project Part 2: Todo App — State & Persistence on release devices through startup, JS/UI frame rate, memory, networking, crash recovery, permissions, signing, and store requirements—not only a debug build. Recheck New Architecture and dependency compatibility on every upgrade.
Read the code step by step
- project requirements
import { useState, useEffect } from 'react';We'll turn the todo array into state with useState([]) and write three functions — addTodo, toggleTodo, and deleteTodo. addTodo appends immutably with the spread operator ([...todos, newTodo]), toggleTodo uses .map() to flip just the done field on the target item, and deleteTodo uses .filter() to remove the target id. With a useEffect that auto-saves to AsyncStorage every time the todo array changes, your data will still be there each time you close and reopen the app.
- components, state, and services
}, []);Evaluate Mini Project Part 2: Todo App — State & Persistence on release devices through startup, JS/UI frame rate, memory, networking, crash recovery, permissions, signing, and store requirements—not only a debug build. Recheck New Architecture and dependency compatibility on every upgrade.
- tested UI
}Test an original Mini Project Part 2: Todo App — State & Persistence example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
- project requirements
- components, state, and services
- tested UI
Success
Explain the React and native-platform behavior of Mini Project Part 2: Todo App — State & Persistence
Failure
Running the save useEffect without a loading flag, which can overwrite AsyncStorage with an empty array (before the initial load even finishes)
Let's think about it this way for a moment
We'll turn the todo array into state with useState([]) and write three functions — addTodo, toggleTodo, and deleteTodo. addTodo appends immutably with the spread operator ([...todos, newTodo]), toggleTodo uses .map() to flip just the done field on the target item, and deleteTodo uses .filter() to remove the target id. With a useEffect that auto-saves to AsyncStorage every time the todo array changes, your data will still be there each time you close and reopen the app.
Let's connect it to a real scenario
On app mount, first load the todo list from AsyncStorage with useEffect(() => { loadTodos() }, []). Then use a separate useEffect (deps: [todos]) to auto-save with AsyncStorage.setItem every time the todo array changes. Watch out here — the save effect might end up re-writing right after the initial load and create an infinite loop, so it's best to keep them clearly separated with a loading state flag.
Code Example
import { useState, useEffect } from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';
function useTodos() {
const [todos, setTodos] = useState([]);
const [loaded, setLoaded] = useState(false);
useEffect(() => {
AsyncStorage.getItem('todos').then((json) => {
if (json) setTodos(JSON.parse(json));
setLoaded(true);
});
}, []);
useEffect(() => {
if (loaded) AsyncStorage.setItem('todos', JSON.stringify(todos));
}, [todos, loaded]);
const addTodo = (text) =>
setTodos([...todos, { id: Date.now().toString(), text, done: false }]);
const toggleTodo = (id) =>
setTodos(todos.map((t) => (t.id === id ? { ...t, done: !t.done } : t)));
const deleteTodo = (id) => setTodos(todos.filter((t) => t.id !== id));
return { todos, addTodo, toggleTodo, deleteTodo };
}After adding, ticking, or deleting a todo item, the data will still be there when you close and reopen the app.5-Minute Try-It
Wire the useTodos hook into Part 1's UI and implement Add button press → addTodo, checkbox press → toggleTodo.
A Quick Word of Caution
If you mutate state directly (todos.push(newTodo)), React can't detect the change and won't re-render — always build a new array/object first, then call setTodos.
Real-world usage
How to apply it in a real project
Test an original Mini Project Part 2: Todo App — State & Persistence example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
Mini challenge
Build it yourself
Test an original Mini Project Part 2: Todo App — State & Persistence example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
import { useState, useEffect } from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';
function useTodos() {
const [todos, setTodos] = useState([]);
const [loaded, setLoaded] = useState(false);
useEffect(() => {
AsyncStorage.getItem('todos').then((json) => {
if (json) setTodos(JSON.parse(json));
setLoaded(true);
});
}, []);
useEffect(() => {
if (loaded) AsyncStorage.setItem('todos', JSON.stringify(todos));
}, [todos, loaded]);
const addTodo = (text) =>
setTodos([...todos, { id: Date.now().toString(), text, done: false }]);
const toggleTodo = (id) =>
setTodos(todos.map((t) => (t.id === id ? { ...t, done: !t.done } : t)));
const deleteTodo = (id) => setTodos(todos.filter((t) => t.id !== id));
return { todos, addTodo, toggleTodo, deleteTodo };
}Expected: Explain the React and native-platform behavior of Mini Project Part 2: Todo App — State & Persistence
Show hint
Test an original Mini Project Part 2: Todo App — State & Persistence example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- project requirements → components, state, and services → tested UI
- Evaluate Mini Project Part 2: Todo App — State & Persistence on release devices through startup, JS/UI frame rate, memory, networking, crash recovery, permissions, signing, and store requirements—not only a debug build. Recheck New Architecture and dependency compatibility on every upgrade.
- Running the save useEffect without a loading flag, which can overwrite AsyncStorage with an empty array (before the initial load even finishes)
- Test an original Mini Project Part 2: Todo App — State & Persistence example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
React Native Documentation — React Native