Skip to content

QuizNot attempted

React.memo with useCallback

Clicking the counter button 3 times — how many times does 'Child rendered' log (including mount)?
function Child({ onClick }) {
console.log('Child rendered');
return <button onClick={onClick}>click</button>;
}
const MemoChild = React.memo(Child);
function Parent() {
const [count, setCount] = useState(0);
const handleClick = useCallback(() => console.log('clicked'), []);
return (
<div>
<MemoChild onClick={handleClick} />
<button onClick={() => setCount(c => c + 1)}>{count}</button>
</div>
);
}
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • 1
  • useCallback(fn, []) returns the same function every render, so MemoChild's props are shallow-equal and memo skips it: only the mount render logs.
  • Not 4 or 3: those are the outcomes without a stable callback.
  • Not 2: no click changes any prop of the child.