Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Routing and Application Architecture
Stage 5: Specialization (pick ONE)

Part 03 / Topic 1 of 7

State vs. props: two different things that change

Part 2 drew a hard line: a component may read its props, but never write to them - only a parent re-rendering with a new value ever changes what a prop holds. State is the other half of that line: a value a component owns for itself, that it can change itself, by calling a setter function useState hands back. examples/01_state_vs_props.jsx puts both side by side:

// examples/01_state_vs_props.jsx
export function StepBadge({ step }) {
  return <p>Step size: {step}</p>;
}

export function ClickCounter() {
  const [count, setCount] = useState(0);

  return (
    <div className="click-counter">
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Add one</button>
    </div>
  );
}

StepBadge has no button, no state, no way to change step on its own - the only way its text ever changes is its parent re-rendering it with a different prop. Re-rendering <StepBadge step={1} /> with step={5} really shows the text move from "Step size: 1" to "Step size: 5" (examples/01_state_vs_props.test.jsx) - but that move came entirely from outside StepBadge. ClickCounter, by contrast, changes its own displayed count by calling setCount from inside its own click handler - nothing external is involved. Rendering two ClickCounters side by side and clicking only the first one's button really proves state is per component instance, not shared or global (examples/01_state_vs_props.test.jsx):

two independent ClickCounters, only the first one's button clicked twice
→ first counter: "Count: 2"
  second counter: "Count: 0"

useState(0) is ClickCounter asking React for one slot of memory, starting at 0, plus a function (setCount) that is the only sanctioned way to change what is in that slot. count itself is never reassigned directly anywhere in this file.

Done here? The practice set for Part 3 — including the solutions — is on the part page.

Search the guide