Faragiri

Learn React Frontend frameworks

A measured stale closure, one render for both pairs, and the three places a value can live where only one of them is state.

4Sections
3Min read
4Depth

Recommended Before This

0 / 1

Two calls, two different answers, one render each. Measured with React 18.3.1
in a browser on 2026-09-02, calling setSold(sold + 1) twice in one
handler moved the count from 0 to 1. Calling setSold(s => s + 1)
twice moved it from 1 to 3. Both produced exactly one re-render.

The reason is that sold inside the handler is a value captured
when that render ran, not a live reading. Two calls therefore both compute the
same number and the second overwrites the first. The function form is handed the
pending value instead, so the two calls compose. The rule: when the next state
depends on the current state, pass a function.

One render for two calls, which is the other half

The log shows one render entry after each pair, not two. React batches state
updates within an event and re-renders once, which is why reading state
immediately after setting it gives the old value: the render has not happened
yet and the variable in scope is from the previous one.

One event, one render

Why reading state right after setting it still shows the old value

The variables inside an event handler stay frozen at the render that created them until React processes the queue and runs the component again.

  1. 01Render captures a snapshotThe sold variable holds the number from the current render and never mutates in place
  2. 02Handler queues state updatesEach setSold call schedules a request for the next render rather than assigning now
  3. 03Inline log reads the old valueA console.log or timeout in the same handler still sees the captured snapshot
  4. 04React batches into one renderThe event finishes and React runs the component once with the composed result

Updater functions like s => s + 1 receive the pending queued value rather than the frozen snapshot, which is why two updater calls add two while two direct value calls overwrite each other.

Interactive Practice Lab

Edit and run this snippet directly in your browser (zero setup)

JavaScript
const items = ['plan', 'build', 'test', 'ship'];items.forEach((step, i) => console.log((i + 1) + '. ' + step.toUpperCase()));

Live Big-O Complexity & Binary Search Race Lab

Compare complexity curves at scale and watch Linear vs Binary Search race live.

That single fact explains most React confusion. State is not a variable you
assign, it is a request for the next render, and the current render's variables
are a snapshot that will never change. Everything else follows: why a
console.log after setState shows the old number, why a
timeout closing over state sees a stale value, and why the function form exists
at all.

Three places a value can live, and only one is state

  • State, for anything that changes and affects what is
    rendered. Changing it re-renders
  • A ref, via useRef, for anything that changes
    and does not affect the output: a render counter, a timer id, a DOM node.
    Changing it re-renders nothing
  • A derived value, computed during render from state and
    props. It is not state, it does not need useState, and storing it
    in state is the most common source of two values that disagree

The derived case is worth naming as a rule: if a value can be computed from
what you already have, compute it. A filtered list in state alongside the full
list is two things to keep in step, and they will not stay in step.

One value, two questions

Where a value belongs, decided before you reach for useState

Only one cell is state, and putting a value in the wrong one is the source of both the stale reads and the extra renders.

it changes while the page is openit does not change

it affects what is rendered

useStateThe only cell that is state. Changing it schedules a render
  • the seat count, a filter, an open panel
A derived valueComputed during render. Storing it in state creates a second truth
  • the filtered list, a total, a formatted date

it does not

useRefChanges freely and renders nothing
  • a timer id, a DOM node, a render counter
A module constantOutside the component entirely, and not a hook at all
  • a lookup table, a configuration value

The top right cell is the one people fill with useState by habit. A value computed from state and props is already correct on every render, and giving it a second home is what lets the two disagree.

What to run in 2026

The React DevTools extension is the tool, free, and its profiler answers the
question this page raises: which components re-rendered and why. The why column
names the prop or the state that changed, which is the difference between fixing
a render problem and guessing at memo calls. It replaced adding a console log to
every component.

What to run and what to drop

The 2026 workflow for diagnosing renders and trimming effects

Use the profiler to find what actually caused a render, and reserve useEffect strictly for external synchronization.

2026 React practice

  1. YesReact DevTools profilerShows which components re-rendered and names the exact prop or state that changedYes
  2. YesuseEffect for external systemsSynchronizes React with something outside it after renderYes
  3. NouseEffect for derived valuesCompute values from props or state directly during render insteadNo
  4. NouseEffect for user actionsRun action logic directly inside the event handler that triggered itNo
  5. NoConsole logs in every componentReplaced by the profiler why column instead of guessing with memoNo

Cutting unnecessary effects removes extra render passes before memoization is even needed.

useEffect is the one to use less of, and that is the current
guidance rather than a preference. An effect that computes something from props
belongs in the render as a derived value; an effect that responds to a user
action belongs in the handler. What is left is synchronising with something
outside React, which is what it is actually for, and it replaced the lifecycle
methods of class components with something smaller and more easily
misapplied.

Where this stalls people

The stall is treating a component as an object with methods rather than as a
function that runs again. Under the object picture, state is a field, setting it
changes it, and everything on this page is inexplicable. Under the function
picture, the component runs from the top on every render, its variables are new
each time, and state is the only thing carried across.

The check that ends confusion

Object picture versus function picture when state updates

Ask yourself what happens between calling setSold and the screen updating to see which mental model you are using.

How do you picture a component between setSold and the screen updating?
  • As an object with mutable fields
    The model breaks on snapshotsTreating state like a field you assign makes stale closures and batched counts look like bugs
    • Expects immediate variable mutation
    • Surprised by stale timeouts and logs
    • Guesses at memo without knowing why renders fire
  • As a function called from the top
    Every render rule clicks into placeLocal variables are fresh on every call and only state is carried across renders
    • Snapshots explain inline console logs
    • Updater functions compose cleanly
    • Stale closures become predictable

Once the component is seen as a function that re-runs from the top, everything from stale closures to shared state across siblings follows from scope.

The check that reveals which model you are holding: say what happens between
the setSold call and the screen updating. If the answer mentions
the component running again from the top, the model is right, and the stale
closure stops being a surprise. The rendering machinery underneath is
building an interface out of
components
, the closure that captured the value is
arguments,
returns, and where scope ends
, and what to do once several components need
the same value is
where shared
state lives once the app grows
.

Above this