easycodingjavascript~8 min
Shallow Equality for memo
Objective: Know exactly what a shallow comparison does and does not catch.
`React.memo` compares props shallowly. Implement `shallowEqual(a, b)` that behaves the same way.
- Two values that are `Object.is`-equal are equal.
- Otherwise both must be non-null objects with the same keys and `Object.is`-equal values per key.
- Nested objects are compared by reference, not deeply.
- NaN equals NaN; +0 and -0 differ (Object.is semantics).
JavaScript
⌘/Ctrl + Enter to run tests · Esc then Tab leaves the editor
Output
> Nothing has run yet.
Interview follow-up questions
- Why does an inline object or arrow function prop defeat React.memo?
- When does useMemo/useCallback actually help memo?
- What does the React Compiler change about this?