MediumHooks & Closures💻 Output Question

Custom hook returns fresh state each call — no sharing

💡

Hint

Custom hooks share logic, not state. Each component that calls a custom hook gets its own isolated state — this is a critical distinction from Context.

What does this output?

// Custom hooks share LOGIC, not STATE
function useCounter(initial = 0) {
  let count = initial;
  return {
    increment() { count++; },
    get()       { return count; },
  };
}

// Two components each call useCounter — independent state
const compA = useCounter(0);
const compB = useCounter(10);

compA.increment();
compA.increment();
compB.increment();

console.log(compA.get());
console.log(compB.get());
console.log(compA.get() === compB.get());

Correct Output

2
11
false

Why this output?

Explanation: Each useCounter() call creates a new closure with its own count variable. compA and compB are completely independent.

Key Insight: Custom hooks share logic, not state. Each component that calls a custom hook gets its own isolated state — this is a critical distinction from Context.

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz