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

Part 06 / Topic 1 of 7

What is (and isn't) a side effect

Rendering a component is supposed to be a pure calculation: given the same props and state, produce the same JSX, with nothing escaping outside that calculation. A side effect is exactly the thing that breaks that rule on purpose - reaching out to something React does not own: the browser's document, a timer, a WebSocket, localStorage, an analytics call. examples/01_document_title_sync.jsx puts a real example of each kind side by side:

// examples/01_document_title_sync.jsx
export function OrderStatusTab({ orderId }) {
  const [status, setStatus] = useState("placed");

  useEffect(() => {
    document.title = `Order #${orderId} - ${status}`;
  }, [orderId, status]);

  return (
    <div>
      <p>Status: {status}</p>
      <button onClick={() => setStatus("preparing")}>Mark preparing</button>
      <button onClick={() => setStatus("out for delivery")}>Mark out for delivery</button>
    </div>
  );
}

export function SavingsBadge({ mrp, price }) {
  const savings = mrp - price;
  return <p>You saved โ‚น{savings}</p>;
}

OrderStatusTab needs to write to document.title - the browser tab's title is not part of the JSX this component returns, it is a piece of the outside world, so the write belongs inside useEffect. SavingsBadge needs nothing of the sort: savings is fully computable from mrp and price, two props this component already has on every render, so it is a plain variable, no hook involved at all. Clicking "Mark preparing" on a rendered OrderStatusTab really does update document.title to "Order #1234 - preparing" (examples/01_document_title_sync.test.jsx); rerendering SavingsBadge with a new price really shows a new savings figure immediately, with no effect, because there was never anything to synchronize - the value was computable all along.

๐Ÿ” Going deeper: this is also why useEffect is taught in its own part, deliberately separate from useState (Part 3) - see DESIGN-NOTES.md. Mixing "state" and "side effects" into one lesson is the most common source of the exact mistake Section 5 below covers: reaching for an effect to compute something that should have been computed during render in the first place.

Done here? The practice set for Part 6 โ€” including the solutions โ€” is on the part page.

Search the guide