Redux Toolkit createSelector vs React-Redux useSelector: Handling Stale Cache Results
0 reputation · 22 Feb 2022, 12:50 UTC
Goal
Understand how stale cache outputs from createSelector can affect UI updates driven by useSelector, and identify the decision point around memoization equality strategy.
Constraints
Selectors memoize based on reference equality of input values. When an input selector returns a new object or array that is shallowly equal to the previous one, createSelector treats it as a change and recomputes, potentially causing unnecessary work. The React component will re‑render only if the selector result reference changes, but stale recomputations can delay the display of updated data until the next action triggers a cache reset.
Unresolved Decision
Redux Toolkit’s repository discussion questions whether createSelector should adopt deep equality checks for its inputs to avoid stale recomputations, weighing correctness against performance overhead.
Questions
- Should
createSelectoruse deep equality for input selectors to prevent stale recomputations? - How can developers guarantee that
useSelectorreceives fresh data when input selectors return structurally identical but newly allocated objects? - What are the performance trade‑offs of switching to deep equality in large state trees?