UI Stale After In-Place State Mutation in Redux Reducer
0 reputation · 28 Jul 2023, 17:56 UTC
0 reputation · 28 Jul 2023, 17:56 UTC
34275 reputation · 28 Jul 2023, 21:48 UTC
No. A reducer that mutates in place and returns the same root reference does not always prevent a useSelector re-render in React Redux v7 or v8. Re-render is decided by the equality check on the selector result, not by the root state reference alone.
React Redux notifies subscribers on every dispatch. useSelector runs the selector on each store update and re-renders only if the selected value differs from the previous value per the equality function. The check is on the selector result, not on root state reference alone.
With default === equality, a selector that returns a mutated nested object reference will not cause a re-render because the reference is unchanged. A selector that returns a primitive derived from mutated nested data will re-render because the primitive value changes, even though the root reference is unchanged.
React Redux v7 and v8 use the same selector equality semantics. v8 adds automatic batching and concurrent features, not a change to reference equality or selector comparison logic.
Redux DevTools trace option records action stack traces for debugging. It does not alter subscription, selector execution, or equality checks and does not force shallow comparison.
Mutation violates Redux immutability. The UI is stale because your selector returns the same object reference that was mutated in place. The default === check sees no change, so React Redux skips the render.
If your selector returns a primitive like state.user.name, you would see a re-render despite the mutation, which masks the bug and will break when the selector shape changes.
Object.is and reference identity across dispatches.One missing diagnostic detail that changes the recommendation: does your useSelector return the nested object reference itself, or a primitive/derived value from it? If it returns the object reference, the default equality will always block updates with in-place mutation. If it returns a primitive, you will see intermittent updates that will break on refactor.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.