ReactEffects & dataLesson 34 / 69

useEffect, gently

Fill-gap25 min

useEffect, gently

Some work cannot happen during render: timers, subscriptions, fetching data. useEffect runs after render:

useEffect(() => {
  document.title = `Progress: ${done}/${total}`;
}, [done, total]);

The array at the end is the deal you make with React: "only re-run when these change." Empty array → run once. No array → run every render (almost always wrong).

The common over-reach is putting things in effects that could be computed during render. Rule: if it can be derived from state, derive it; effects are for reaching outside React.

Exercise

Sync the document title with a counter's value using useEffect. Then remove the effect and derive the title instead — explain which is right and why.