Sub topic 3
Referential equality: when two values count as the same
React decides whether to skip work by asking one question again and again: is this value the same as last time? This chapter explains how JavaScript answers that question, because React.memo, useMemo, and useCallback all depend on the answer. It assumes you know function components, JSX, useState, and how props are passed.
Primitives compare by value, objects by reference
JavaScript has two kinds of values. Primitives (numbers, strings, booleans, null, undefined) are compared by their content. Objects, arrays, and functions are compared by reference, meaning by their identity in memory.
console.log(1 === 1); // true
console.log('blue' === 'blue'); // true
console.log({ color: 'blue' } === { color: 'blue' }); // false
console.log([1, 2] === [1, 2]); // false
console.log((() => {}) === (() => {})); // false
const a = { color: 'blue' };
const b = a;
console.log(a === b); // true: same objectThe two object literals look identical, but each literal creates a new object. Only a and b point to the same one.
What React actually uses: Object.is
React does not use === directly. It uses Object.is, which behaves the same for almost every value. The two differences are edge cases.
| Expression | `===` | `Object.is` |
|---|---|---|
| `NaN` vs `NaN` | `false` | `true` |
| `0` vs `-0` | `true` | `false` |
| `{}` vs `{}` | `false` | `false` |
For everyday props and state, you can think of it as ===.
Why this matters for renders
A function component runs from top to bottom on every render. Every object, array, or function literal inside it is created again each time, so each render produces a new reference, even when the contents are unchanged.
function Dashboard() {
const options = { color: 'blue' }; // new object on every render
const onSave = () => {}; // new function on every render
return <Chart options={options} onSave={onSave} />;
}Seen from Chart, the options and onSave props are different values after each render of Dashboard. Nothing about their content changed, but their identity did.
Where React applies the comparison
React runs this same check in three places.
- State updates. If you call
setStatewith a value that isObject.is-equal to the current state, React skips re-rendering that component. - Props, in
React.memo. A memoized component compares each prop with its previous value and skips rendering if all are equal. This is a shallow comparison: it checks each prop's reference, not what is inside it. - Dependency arrays.
useMemo,useCallback, anduseEffectcompare each dependency with its previous value to decide whether to recompute or re-run.
Common errors
Mutating state in place leaves the reference unchanged, so React sees no change:
import { useState } from 'react';
export function Todos() {
const [todos, setTodos] = useState(['write']);
function addBroken() {
todos.push('test'); // same array, same reference
setTodos(todos); // Object.is says equal: no re-render
}
function addFixed() {
setTodos([...todos, 'test']); // new array, new reference
}
return (
<>
<button onClick={addBroken}>Broken add</button>
<button onClick={addFixed}>Fixed add</button>
<p>{todos.join(', ')}</p>
</>
);
}The opposite mistake is creating a new reference when nothing changed. That is the cause of most "memo does nothing" bugs, and later chapters show how to fix it.
Exercise
A parent renders <Row style={{ margin: 4 }} /> and Row is wrapped in React.memo. The parent re-renders because of unrelated state. Does Row re-render, and why?
Show answer
Yes. The object literal creates a new reference on every parent render, so Object.is reports the style prop as changed even though its contents are identical. memo compares references, so it cannot skip the render.
Exercise
What does Object.is(NaN, NaN) return, and how does that differ from NaN === NaN?
Show answer
Object.is(NaN, NaN) returns true, while NaN === NaN returns false.