Chapters & contents Lesson 2 of 12
3 min read
ReactExpanded reading

State belongs to a render

Predict interface changes by tracing the state used by each render.

What you’ll learn

  • Distinguish rendering from an event
  • Use an updater for dependent state changes
  • Keep derived values out of duplicated state

Describe the screen from data

A React component describes an interface using its current inputs and state. Treat a render as a calculation of the screen for those values, rather than a place to start unrelated work. For example, a reading counter can display the stored count and provide a button that requests a change. The rendered label and the event handler are produced together. Thinking in these concrete snapshots makes delayed callbacks easier to reason about than imagining the component as one continuously changing object.

An event uses the values it captured

If an event handler was created when count was two, the count variable inside that handler remains the value from that render. Requesting a state update does not rewrite that variable in the already executing function. Calling an update with count plus one twice can therefore request the same next value twice. When a new value depends on the previously queued value, an updater function expresses that relationship clearly and avoids relying on a stale captured count.

Trace one interaction completely

Imagine a button that adds two completed pages. Start with count equal to two. The handler queues an increment updater and then another increment updater. Each updater receives the result of the preceding queued update, so the next count becomes four. React can then render the new label. The useful question is which value each expression receives, not how many source-code lines ran. Write that small trace before making claims about render counts, which are not a reliable application contract.

jsx · Worked example
setCount(value => value + 1);
setCount(value => value + 1);
// Each updater receives the result of the preceding queued update.

Store only the independent facts

If the interface shows a list and the number of visible items, the count can often be calculated from that list during rendering. Storing both independently creates another rule that every update must preserve. Likewise, do not directly mutate an object stored in state and expect the operation to communicate a new value correctly. Build the next value according to the update API. Keeping state small reduces the number of combinations that tests and readers must understand.

Check user-visible transitions

Test the initial label, a single click, and several quick clicks. If asynchronous work is involved, test the selected item changing before the result returns. Use the framework documentation matching your project for effects and data-loading patterns, because those APIs and recommendations evolve. The stable foundation is the same: rendering describes a state, events request changes, and the interface must remain meaningful while work is pending or fails.

Try it yourself

Starting from count = 2, why might two calls to setCount(count + 1) produce 3 rather than 4?

Show a possible solution

Both calls can use the same captured count of 2 and request 3. Use two updater calls, setCount(value => value + 1), when each increment should depend on the previously queued value.

Check your understanding

Try 11 questions across react. Read each explanation and revisit anything that needs another pass.

Practise this subject

Go to the source

Use these primary references to check details and explore the topic further.

Saved in this browser. Signed-in progress syncs to your account.