Skip to content

QuizNot attempted

React.memo without 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 = () => 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:

  • 4
  • React.memo shallow-compares props, but handleClick is a new function on every Parent render, so onClick always differs and MemoChild re-renders each time: 1 + 3 = 4.
  • Not 1: that needs a stable onClick (useCallback).
  • Not 3: the mount render also logs.
  • Not 0: memo never skips the first render.