Sub topic 11
Custom comparison functions and their risks
This chapter assumes you know how React.memo compares props shallowly with Object.is, and why inline objects and functions break that check. It covers the second argument to memo, when it helps, and how it causes stale UI.
What the second argument does
By default, memo compares each prop with Object.is. If every prop is equal, React skips the render. You can replace that check by passing a function as the second argument to memo.
The function receives the previous props and the next props. It returns true if the two sets are equivalent, so React can reuse the last render. It returns false if the component must render again.
Note the direction. true means "skip", which is the opposite of shouldComponentUpdate in class components.
A correct example
UserCard receives a user object that a parent rebuilds on every render, usually after mapping over API data. The object has a new reference each time, but the fields that matter rarely change. A custom comparison can look inside the object.
import { memo } from 'react';
const UserCard = memo(
function UserCard({ user, onSelect }) {
return <button onClick={() => onSelect(user.id)}>{user.name}</button>;
},
(prevProps, nextProps) =>
prevProps.user.id === nextProps.user.id &&
prevProps.user.name === nextProps.user.name &&
prevProps.onSelect === nextProps.onSelect
);The comparison checks user.id and user.name, which the output uses. It also checks onSelect, which the click handler calls.
The main risk: stale data
If the comparison ignores a prop that the component uses, the component keeps showing old output. Suppose you remove the last line from the example above.
(prevProps, nextProps) =>
prevProps.user.id === nextProps.user.id &&
prevProps.user.name === nextProps.user.nameIf the parent later passes a different onSelect, the card still calls the old one. That old function may close over outdated state. The bug is quiet because nothing throws an error. The UI just behaves as if it were a few renders behind.
Common ways this happens:
- A new prop is added to the component, but nobody updates the comparison.
- Functions are skipped because "they always change".
- Only some fields of an object are compared, and later the component starts using another field.
Other costs
The comparison runs on every parent render, for every instance. A deep comparison of a large array can cost more than the render it avoids. Deep equality functions such as JSON.stringify comparisons are especially risky, because they run on the main thread and can fail on cyclic or non-serializable data.
React also warns against relying on custom comparison for deep checks. The documentation advises that if you write one, it should be fast, and that you should measure with the Profiler to confirm a benefit.
| Approach | Cost per parent render | Staleness risk |
|---|---|---|
| Default shallow compare | Low | None, if props are stable |
| Custom compare on a few fields | Low to medium | Medium, if fields are missed |
| Deep compare | Can be high | Medium, plus slow on big data |
Safer alternatives
Prefer fixing the cause of unstable props, as covered earlier:
- Stabilize objects with
useMemoand functions withuseCallbackin the parent, so the default comparison works. - Pass primitive props such as
userIdanduserNameinstead of a whole object. Primitives compare by value underObject.is. - Move state down or pass
children, so the component does not re-render at all.
Use a custom comparison only when you cannot change the parent, for example when props come from a library you do not control.
Exercise
A memoized Price component takes amount, currency and onBuy. Its custom comparison returns prev.amount === next.amount && prev.currency === next.currency. The parent passes a new onBuy that reads the current cart. What bug can this cause, and how do you fix it?
Show answer
The component skips renders when only onBuy changes, so the button keeps calling an old onBuy that closes over an outdated cart. Fix it by also comparing prev.onBuy === next.onBuy, or better, by removing the custom comparison and wrapping onBuy in useCallback in the parent so the default shallow comparison works.