Selector-based subscriptions with subscribeWithSelector versus whole-store subscriptions for frequent unrelated updates
23.5K reputation · 14 Sept 2026, 20:07 UTC
Choosing a subscription strategy for a React component that reads a stable slice of Zustand state while another slice updates at high frequency. The component must avoid re-rendering on unrelated mutations but remain correct when its slice changes.
subscribeWithSelector enables selector-based subscriptions that notify only when the selected value changes. Whole-store subscriptions are simpler and use the default store listener. Selector equality defaults to reference equality, so shallowly equal objects may still trigger updates unless an explicit equality function is provided. Middleware ordering with devtools and persist can affect when selectors are evaluated.
When is selector-based subscription with default reference equality sufficient for a slice that is derived from an object in the store? What trade-offs appear between selector granularity and the complexity of providing a custom equality function for components that render under frequent unrelated state updates?