Skip to content

QuizNot attempted

Two instances of a custom hook

Clicking the first button twice and the second button once — what do the two buttons show? Do useCounter(0) and useCounter(100) share any state?
function useCounter(initial) {
const [count, setCount] = useState(initial);
const increment = () => setCount(c => c + 1);
return [count, increment];
}
function App() {
const [countA, incA] = useCounter(0);
const [countB, incB] = useCounter(100);
return (
<div>
<button onClick={incA}>{countA}</button>
<button onClick={incB}>{countB}</button>
</div>
);
}
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • 2 and 101 — each call to a custom hook gets its own independent state instance
  • Each custom hook call runs its own useState, so every call gets an independent state slot: A goes 0→2, B goes 100→101.
  • Not 2 and 102: hooks share logic, never state (use context or a store to share state).
  • Not 0 and 100: the setters do update.
  • Not 'throws': calling the same hook twice is normal.