01. A child wrapped with React.memo still re-renders. Why?
📄 Notesmedium#react-memo#memoization#re-renders#performance
Short answer
React.memo only skips a render when every prop is shallowly equal (Object.is) to the previous one. Most often the parent creates a new object, array, function or JSX element on every render, so the comparison fails. The child can also re-render for reasons memo doesn't cover: its own state, a context it reads, or a custom hook that updates.
Why it happens
memo compares props one by one with Object.is. Two values that look the same but are different references fail that check.
| Prop passed by parent | New reference each render? |
|---|---|
count={5} / label="Hi" | No (primitives) |
style={{ color: "red" }} | Yes |
items={data.filter(...)} | Yes |
onClick={() => save(id)} | Yes |
children={<Icon />} / nested JSX | Yes (JSX creates a new element object) |
Re-renders that memo never blocks:
- Own state changes (
useState,useReducer) inside the child. - Context: if the child calls
useContext(ThemeContext)and the provider'svaluechanges, it re-renders regardless of props. - Custom hooks that hold state or subscribe to a store (
useSelector,useSyncExternalStore) trigger renders from the inside. - A changed
keyremounts the component entirely.
How to fix it
- Find which prop changed (React DevTools Profiler, "Why did this render?").
- Stabilize that prop.
import { memo, useCallback, useMemo, useState } from "react";
const Chart = memo(function Chart({ options, onSelect }) {
return <div onClick={() => onSelect(options.type)}>{options.type}</div>;
});
export default function Dashboard({ type }) {
const [count, setCount] = useState(0);
// stable while `type` is unchanged
const options = useMemo(() => ({ type, animate: true }), [type]);
const handleSelect = useCallback((t) => console.log("selected", t), []);
return (
<>
<button onClick={() => setCount((c) => c + 1)}>Clicked {count}</button>
<Chart options={options} onSelect={handleSelect} />
</>
);
}
- Hoist truly static values (
const DEFAULT_OPTIONS = {...}) outside the component. - Pass primitives instead of objects when you can (
type={type}rather thanoptions={{ type }}). - For context, split providers or memoize the
valueobject.
💡 Interview tip: With the React Compiler (stable since v1.0) enabled, the compiler memoizes those objects, callbacks and JSX for you at build time, so most manual
useMemo/useCallbackwiring disappears.
How to verify
- In the Profiler, enable "Record why each component rendered while profiling"; it lists the exact prop or hook that changed.
- Temporarily log inside the child, or use a custom
arePropsEqualthat logs which key differs. - After the fix, interacting with the parent should show the child as "Did not render" (grey) in the flame graph.
Follow-up questions
- Does
memodo a deep comparison? No, it's shallow. You can pass a custom comparator as the second argument, but deep checks can cost more than the render you're skipping. - Why does passing
childrenbreakmemo? JSX like<Icon />makes a new element object each render, so thechildrenprop is never equal. - Is
memoa guarantee? No. React treats it as a performance hint, not a semantic guarantee.
Quick recap
memo= shallowObject.ischeck on each prop.- Inline objects, arrays, functions and JSX break it.
- State, context and store subscriptions bypass it entirely.
- Stabilize props with
useMemo/useCallback, hoisting, or the React Compiler.