Skip to content

QuizNot attempted

A derived boolean as a dependency

Clicking the button 6 times (count goes 0→6), how many total times (including mount) does 'effect ran' log?
function App() {
const [count, setCount] = useState(0);
useEffect(() => {
console.log('effect ran');
}, [count > 3]);
return <button onClick={() => setCount(c => c + 1)}>{count}</button>;
}
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • 2
  • The dependency is the boolean count > 3, which is false for 0–3 and true for 4–6
  • it changes only once (3→4), so the effect runs on mount and once more: 2.
  • Not 1: the false→true flip re-runs it.
  • Not 6 or 7: React compares the boolean, not count, so other clicks don't change the deps.
  • (The lint rule would flag the expression, but the behaviour is well defined.)