liveBook · Manning

Reference equality

As explained in React in Depth

Referential equality is equality determined by whether two values reference the same object or function rather than merely having equivalent contents.

Reference equality determines whether two values are the same object or function, not whether their contents merely look alike. JavaScript primitives compare by represented value with strict equality, while objects, arrays, and functions compare by identity: {} !== {}, and [] !== []. section 3.3

React uses reference equality when checking nonempty dependency arrays. A single changed dependency reruns the hook; an omitted array runs after every render, and an empty array runs only on mount apart from cleanup on unmount. section 3.3

Memoization can preserve a value's reference between renders as well as avoid repeated work. useMemo recalculates only when its dependencies change, while useCallback provides the same pattern for functions. section 3.2.4

Memoizing a filtered employee list

const filteredList = useMemo( () => employeeList.filter(({ isTemporary }) => showTempOnly ? isTemporary : true ), [employeeList, showTempOnly] ); The filtered array is recomputed when either the source array or the filter changes; otherwise, useMemo returns the previous value reference. section 3.2.4

How React in Depth covers it

How deep it goes
Defines primitive and object identity, then applies it to dependency arrays, useMemo, and useCallback without broader React internals.
Examples
Uses empty object and array comparisons, dependency-array forms, and memoized employee filtering.
What it assumes
Assumes React hooks and JavaScript strict equality, while teaching how primitives differ from objects during comparison.
What you can do after
Apply it via React hooks to control recalculation and preserve value or function references across renders.
How it connects
Connects strict equality, dependency arrays, useMemo, useCallback, and React hooks.

Also explained in

Compare how 4 books explain reference equality →

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.