Sub topic 14
How Props Relate to Re-rendering and memo
This chapter assumes you can write function components, pass props (including callback props), and use useState. It covers when a component re-renders, what memo changes, and why new object and function props can defeat it.
The working model
A component re-renders for two reasons: its own state changed, or its parent re-rendered. Changed props are not a trigger. By default, when a parent renders, React renders every child it returns, even if the props are identical to last time.
Re-rendering means React calls your function again and compares the result with the previous output. It does not mean the browser repaints everything. Most re-renders are cheap, so this default is usually fine.
Skipping renders with memo
memo wraps a component so React compares the new props with the previous props before rendering it. If every prop is equal, React reuses the last result.
import { memo, useState } from 'react';
const Row = memo(function Row({ item }) {
console.log('render', item.name);
return <li>{item.name}</li>;
});
const items = [
{ id: 1, name: 'Ada' },
{ id: 2, name: 'Grace' },
];
export default function App() {
const [count, setCount] = useState(0);
return (
<div>
<button onClick={() => setCount(count + 1)}>Clicked {count}</button>
<ul>
{items.map((item) => (
<Row key={item.id} item={item} />
))}
</ul>
</div>
);
}Each click re-renders App, but the Row log lines appear only on the first render. The items array is defined outside the component, so each item keeps the same reference.
Why shallow comparison trips people up
memo compares each prop with Object.is. Strings, numbers, and booleans are compared by value. Objects, arrays, and functions are compared by reference. An object literal or arrow function written inside a component is a new value on every render, so the comparison fails and memo does nothing.
import { memo, useCallback, useState } from 'react';
const Row = memo(function Row({ item, onSelect }) {
return <li onClick={() => onSelect(item.id)}>{item.name}</li>;
});
export default function App() {
const [selected, setSelected] = useState(null);
const [items] = useState([
{ id: 1, name: 'Ada' },
{ id: 2, name: 'Grace' },
]);
const handleSelect = useCallback((id) => setSelected(id), []);
return (
<div>
<p>Selected: {selected ?? 'none'}</p>
<ul>
{items.map((item) => (
<Row key={item.id} item={item} onSelect={handleSelect} />
))}
</ul>
</div>
);
}useCallback returns the same function between renders while its dependency list is unchanged. Here the list is empty, and setSelected is stable, so handleSelect never changes. Use useMemo the same way for objects and arrays computed from other values.
Common errors
- Passing
style={{ color: 'red' }}oronClick={() => ...}to a memoized child. Each render creates a new value, so the child always re-renders. - Passing JSX as
childrento a memoized component. The elements are new objects on every parent render. - Wrapping a component in
memobut mutating props or state objects. Mutation keeps the same reference, so React may skip an update that should happen. Always create new objects. - Adding
useCallbackfor a function passed to a component that is not memoized. It has no effect.
Summary
Props do not cause re-renders. A parent rendering does. memo lets a child skip rendering when its props are shallowly equal, and stable references for objects and functions are what make that work.
Exercise
A memoized Chart component still re-renders every time its parent re-renders. The parent renders <Chart options={{ color: 'blue' }} />. Why does this happen, and how do you fix it?
Show answer
The inline object { color: 'blue' } is a new object on every parent render. memo compares props by reference with Object.is, so it sees a changed prop. Fix it by defining the object outside the component, or by creating it with useMemo(() => ({ color: 'blue' }), []) inside the component. The reference then stays the same between renders and memo can skip the render.