Skip to content

QuizNot attempted

Parent re-render, same primitive dependency

Parent re-renders <App userId={1} /> three times in a row with the same userId value of 1 each time. How many times does 'fetching 1' log?
function App({ userId }) {
useEffect(() => {
console.log('fetching', userId);
}, [userId]);
return <div>{userId}</div>;
}
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • 1
  • React compares each dependency with Object.is
  • 1 is 1, so after the mount run the effect is skipped on every re-render: one log.
  • Not 3: a re-render alone doesn't re-run an effect whose deps are equal.
  • Not 0: effects always run after the first mount.
  • Not 'depends on key': a key change would remount, but the parent passes the same props and no key change is described.