Skip to content

QuizNot attempted

Splitting state and actions contexts

A context value is { value, setValue }. A <SaveButton> only uses setValue. Why does it re-render whenever value changes, and what fixes it?
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • Every consumer re-renders when the context value changes; split into a State context and a memoized Actions context
  • Context has no selectors: when the provided object changes, every consumer re-renders, even one that only reads setValue.
  • Split into a State context and an Actions context whose value is memoized (setters are stable), so action-only consumers never re-render.
  • **Not 'buttons always re-render
  • useCallback':** the cause is the context value, and useCallback on the button changes nothing.
  • Not 'setValue changes identity': useState setters are stable.
  • Not 'only the Provider re-renders': consumers re-render directly on value changes.