Thuta Learning
React Native
ProjectsMobile DevelopmentIntermediate

Mini Project Part 2: Todo App — State & Persistence

React NativeLesson 2930 min

What you'll walk away with

  • Explain the React and native-platform behavior of Mini Project Part 2: Todo App — State & Persistence
  • Test accessibility, failure, and lifecycle cases
  • Write maintainable, testable, and performant React Native

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.

Mental model
  1. project requirements
  2. components, state, and services
  3. tested UI

Explain the React and native-platform behavior of Mini Project Part 2: Todo App — State & Persistence

Core syntax or pattern

javascript
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

  1. project requirementsimport { 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.

  2. 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.

  3. 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.

Execution flow
  1. project requirements
  2. components, state, and services
  3. 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

javascript
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 };
}
You should see
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
javascript
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

What best describes the core idea of Mini Project Part 2: Todo App — State & Persistence?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

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

Easy traps

  • Running the save useEffect without a loading flag, which can overwrite AsyncStorage with an empty array (before the initial load even finishes)
  • Directly mutating the array with .push()/.splice() (stick to the immutable pattern — .map()/.filter()/spread only)
  • Assuming one simulator's happy path proves every real-device, release-build, permission, accessibility, offline, and lifecycle path.

Hands-on Exercise

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.

You'll know it worked when: After adding, ticking, or deleting a todo item, the data will still be there when you close and reopen the app.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Mini Project Part 2: Todo App — State & Persistence | Thuta Learning