Skip to content
FrontDepth, Home

React: how useState works

MiddleSenior

Overview

Follow useState through React 19's source: the dispatcher, the list of hooks on the fiber, mount vs update, the update queue and how a setter schedules one render.

const [count, setCount] = useState(0) is the most used line in React, and it hides a small machine. This tour follows it through React's source at the v19.3.0 tag: from the two-line useState in the react package, through the dispatcher that the renderer switches between mount and update and the list of hooks stored on each component's fiber, to what setCount really does — build an update, try to bail out early, queue it and schedule one render for however many updates you make.

You only need to know how to use useState. Two words come up everywhere: a fiber is React's object for one component instance, and a lane is a bit that marks an update's priority. Where a step says "measured", the code was run with React 19.3.0 in the development and the production build. Left out: useReducer's own dispatch, transitions and useOptimistic, Suspense, and how React walks the tree and commits to the DOM.

How the pieces connect

Select a box to open the step that explains it. Dashed boxes are mentioned but not covered.

Connections:
  • renderWithHooks → useState (sets the dispatcher)
  • useState → mountState (mount)
  • useState → updateState (later renders)
  • updateState → updateReducer (calls)
  • mountState → dispatchSetState (binds)
  • dispatchSetState → enqueueUpdate (queues the update)
  • dispatchSetState → didScheduleMicrotask (scheduleUpdateOnFiber)
  • didScheduleMicrotask → renderWithHooks (one microtask, then render)