State Management · Senior · Question 6 of 75
How do you prevent consumers of a CartContext from re-rendering when only dispatch-style actions are used?
- ✓Correct answer: Provide state and actions in separate contexts; the actions context value is stable
- 2Wrap the provider in
React.memo - 3Store actions in
useRefinside each consumer
Explanation
Buttons that only call addItem read CartActionsContext (memoized once), so cart changes don't re-render them. Only components that read CartStateContext update.