Imagine a searchable product list: typing changes the visible results, clicking “add” updates a count, and switching to another product opens its editor. If a number is wrong or an input still shows the previous product's notes, can you predict where the bug comes from? Use these examples to practice explaining modern React function components with Hooks. Start with what each render can see, not with a proposed optimization.

Trace one interaction before you debug it

1. Why does the handler log the old count after calling the setter?

Each render gets a snapshot of state. The handler from that render closes over that snapshot, so calling setCount(count + 1) requests an update but does not change the local count variable. A log later in the same handler still sees the same number. Even if the handler runs code after an await, it does not acquire a new closure. Ask whether the code should use the snapshot from the original action or the state at a later point; both can be legitimate requirements.

2. Why can three setter calls increase a count by only one?

When all three calls in one handler compute count + 1 from the same snapshot, they all request the same replacement value. To compose changes based on the pending state, pass updater functions. For an initial count of zero, the following handler queues increments to one, two, then three:

function addThree() {
  setCount(previous => previous + 1);
  setCount(previous => previous + 1);
  setCount(previous => previous + 1);
}

Keep the updater pure. If one click should send an analytics event, put that work in the click handler, outside the updater. A useful follow-up is to explain what resetting the count to zero after those calls does: it queues a replacement after the increments, so the resulting count is zero.

Put the work where it belongs

3. Should visible search results live in their own state?

If the results are entirely determined by the current list and search input, calculate them during render. Storing both the input and the filtered output introduces two values that must be kept in sync. An effect that computes the list after commit can leave a render with stale results and request another render just to bring the values back together. If the filter is demonstrably expensive, measure before adding memoization. If the results come from a server, that is different: the response is external data, not a pure calculation from the local list.

4. What belongs in an effect, and what belongs in a handler?

Sending a checkout request belongs in the submit handler because it answers “what should happen when the user does this?” Subscribing to updates for the currently selected product can belong in an effect because it answers “what should stay synchronized while this product is displayed?” Give the effect every reactive value it reads, and clean up the old subscription when that product changes or the component unmounts.

useEffect(() => {
  const unsubscribe = subscribeToProduct(productId, onUpdate);
  return unsubscribe;
}, [productId, onUpdate]);

Here subscribeToProduct is a module-level function and onUpdate is received as a prop. If the parent passes a different callback on each render, the effect will resubscribe. Do not hide that behavior by suppressing the dependency. First ask whether the parent can make the callback stable or whether the child needs a different subscription design. In development, Strict Mode may run an extra setup and cleanup cycle to expose unsafe synchronization.

5. Why does an editor still show the last product's draft?

React associates state with a component's place in the rendered tree, taking its type and key into account. If the same editor occupies the same place after the ID changes, its local draft state can persist. When a product change should discard the local draft, render <ProductEditor key={productId} productId={productId} />. If drafts must survive switching products, a reset is the wrong fix: store the drafts by product ID in an appropriate owner. The same identity principle explains why index keys in a reorderable list can make a row's local state follow the position rather than the item.

Practice on one small interface

  • Write down the values held by a render before you run the counter example, then predict the next displayed count.
  • Delete duplicate state from a list filter. Explain which inputs still need to be stored and who owns them.
  • Switch between two products. Predict when a subscription starts, when it stops, and what happens to unsaved notes.
  • Repeat the key question with a new requirement: preserve notes for each product while switching between them.

For more on practicing explanations, see why recognizing an answer is not the same as being able to explain it. Then try Mentaro's free practice demo and answer a sample card aloud before revealing its explanation.

Sources