STAGE 02 / LESSON 03
React state and effects
State remembers information that can change between renders.
THE IDEA
Understand it.
Use state for values the UI needs to remember, such as whether a task is complete. Calling a state setter schedules a render with a new value. When the next value depends on the previous one, use the updater form.
An effect synchronizes a component with something outside React, such as a browser subscription. Derive simple values during rendering instead of using an effect to duplicate state. Clean up subscriptions when the effect ends.
SEE IT IN CONTEXT
Toggle a task
"use client";
import { useState } from "react";
function DoneButton() {
const [done, setDone] = useState(false);
return (
<button onClick={() => setDone(previous => !previous)}>
{done ? "Done" : "Mark done"}
</button>
);
}React re-renders when state changes. The updater receives the previous value, avoiding reliance on a stale snapshot.
LEARN FROM REAL WEBSITES
See it in the real world.
These simplified structures come from public sources. Company systems change, and the sources may describe only one project or an earlier design.
GitHub
Public code checked November 2026Interaction state in a button
Primer Brand's public button component uses React state for interaction and includes pointer and focus handlers.
- Interaction
- Event handler
- React state
- Rendered button
What to learn: State connects an event to the next rendering of the component. The source is a specific open-source button.
Read the source: Primer Brand button sourceSpotify
Spotify engineering, 2021Hooks expose platform capabilities
Spotify described exposing platform APIs to its React interface through React Hooks in its shared client architecture.
- React UI
- React Hooks
- Platform APIs
What to learn: Hooks can connect components to capabilities outside their immediate markup; the report does not specify every effect or state value.
Read the source: Spotify desktop and web architectureTRY IT YOURSELF
Practice.
The interface needs a count of completed tasks. Store another count state or calculate it from the task list?
Show one possible answer
Usually calculate it from the task list during rendering. That avoids keeping two values in sync.
GO DEEPER