liveBook · Manning

Stable value

As explained in React in Depth

A stable value is a value whose identity remains unchanged across renders of a component.

A stable value keeps the same identity across component renders. It can be omitted from a hook dependency array when its reference is known never to change. This matters especially for functions and objects: identical behavior or structure does not guarantee identical identity. section 3.3.3

Common stable values include:

  • Constants defined outside a component or imported from another file. section 3.3.3
  • The setter returned by useState, whose function reference remains stable even when the state value changes. section 3.3.3
  • The object returned by useRef, whose identity remains stable while its current property remains dynamic. section 3.3.3
const [items, setItems] = useState(["Clean gutter", "Do dishes"]);
...
const onDelete = useCallback(
  (item) => setItems(     #1
    (list) => list.filter((i) => i !== item),
  ),
  []     #2
);

Shows useCallback omitting the stable useState setter from its dependency array. section 3.3.3

How React in Depth covers it

How deep it goes
Explains reference identity through constants, functions, objects, useState setters, and useRef, without developing broader rendering internals.
Examples
Uses external constants, a useState setter, a useRef object, and a useCallback handler with an empty dependency array.
What it assumes
Assumes React components, renders, hooks, and dependency arrays while defining stable identity through practical cases.
What you can do after
Evaluate hook dependency arrays by identifying references that remain unchanged across renders.
How it connects
Connects stable values to dependency arrays, React hooks, useState, useRef, useCallback, and useEffect.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React in Depth. The section links point to the parts of the book it draws on.