HardMemoization & Identity💻 Output Question

Context re-render — every consumer re-renders on value change

💡

Hint

React Context has no built-in selector optimization. When any context value changes, ALL consumers re-render. Use separate contexts per concern, or state management tools like Zustand that have subscription-based selectors.

What does this output?

let renders = { A: 0, B: 0, C: 0 };

// Simulates context provider with value
function createProvider(value) {
  return {
    subscribe(component, selector) {
      return {
        render() {
          renders[component]++;
          return selector(value);
        }
      };
    }
  };
}

// Round 1: provider with { user, theme }
const v1 = { user: 'Alice', theme: 'dark' };
const p1 = createProvider(v1);
const subA = p1.subscribe('A', v => v.user);
const subB = p1.subscribe('B', v => v.theme);

subA.render(); subB.render();

// Round 2: only theme changed, but ALL consumers re-render (no selectors in Context)
const v2 = { user: 'Alice', theme: 'light' }; // new object, user unchanged
const p2 = createProvider(v2);
const subC = p2.subscribe('C', v => v.user); // same user

subC.render(); // re-renders even though user didn't change

console.log(renders.A);
console.log(renders.B);
console.log(renders.C);

Correct Output

1
1
1

Why this output?

Explanation: All three subscriptions render once each. In real React Context, ALL consumers re-render when the context value changes, regardless of which field they use.

Key Insight: React Context has no built-in selector optimization. When any context value changes, ALL consumers re-render. Use separate contexts per concern, or state management tools like Zustand that have subscription-based selectors.

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz