Course contentsShow
← React
Lesson 1501 of 2,39637. React 19 and BeyondFree lesson

useFormStatus + useOptimistic Together

Combine both hooks to create forms with instant feedback and loading states, the complete modern React form experience.

useFormStatus + useOptimistic Together

What you'll learn: How to combine useFormStatus and useOptimistic to build forms that show both instant feedback and accurate loading states.

The Complete Picture

You've learned useFormStatus (reading form submission state) and useOptimistic (showing instant updates). Now you'll combine them for the ultimate user experience: instant UI changes when users submit, while also showing proper loading states.

Think of it like ordering at a modern coffee shop. The barista immediately writes your name on a cup (optimistic update), but the order screen also shows "Processing payment..." (pending state). You get instant feedback and transparency about what's happening.

The Pattern

Here's how they work together:

function TodoList({ todos }) {
  const [optimisticTodos, addOptimisticTodo] = useOptimistic(
    todos,
    (state, newTodo) => [...state, { ...newTodo, pending: true }]
  );

  async function createTodo(formData) {
    const text = formData.get('todo');
    addOptimisticTodo({ id: crypto.randomUUID(), text });
    await saveTodo(text);
  }

  return (
    <>
      <ul>
        {optimisticTodos.map(todo => (
          <li key={todo.id} style={{ opacity: todo.pending ? 0.6 : 1 }}>
            {todo.text}
          </li>
        ))}
      </ul>
      <form action={createTodo}>
        <TodoInput />
      </form>
    </>
  );
}

function TodoInput() {
  const { pending } = useFormStatus();
  
  return (
    <>
      <input name="todo" disabled={pending} />
      <button disabled={pending}>
        {pending ? 'Adding...' : 'Add Todo'}
      </button>
    </>
  );
}

The optimistic update shows the new todo immediately. Meanwhile, useFormStatus disables the form and shows loading text. Users see instant results and clear feedback that work is happening.

Key Takeaway: Combining useFormStatus and useOptimistic gives you the best of both worlds—instant UI updates for perceived speed and accurate loading states for transparency. This is the modern React form pattern.