Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

useSelector keeps re-rendering a component although the data did not change

ErrorA component re-renders after every dispatched action, even unrelated ones

Short answer

useSelector compares the value your selector returns with the previous one using ===. If the selector builds a new object or array on every call, or the reducer swaps in new references for unchanged data, the comparison fails after every action and the component renders again. Return stable references instead.

  • Applies to React 19, React Redux 9, Redux Toolkit 2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log what your selector returns and compare it with the previous value using ===. A false for identical-looking data tells you the selector is creating a new reference.

    const prev = useRef();
    const value = useSelector(selectThing);
    console.log('same reference?', prev.current === value);
    prev.current = value;
  2. 2Match what you see, then jump to its fix

    1. 1The selector contains an object literal, [...], .map() or .filter() inside the function passed to useSelectorThe selector returns a new object or array on every callFix
    2. 2The selector ends in .filter(...) or .map(...), so its result is a fresh array each timeDerived data (filter, map, sort) is computed inside the selectorFix
    3. 3useSelector((state) => state), or a selector that reads one big slice but the component uses two fieldsThe selector returns the whole state, or a large sliceFix
    4. 4The selected value is a plain slice or item, yet it still changes identity after unrelated actionsA reducer returns new references for data that did not changeFix

What does the error look like?

  • A component re-renders after every dispatched action, even unrelated ones
  • The React Profiler lists the component in every commit, including commits caused by unrelated state

Cause 1 of 4

The selector returns a new object or array on every call

1Check

The React Redux docs state that returning a new object from a selector forces a render after every dispatched action, because the default equality check is strict reference equality. Search for selectors that build objects.

grep -rn "useSelector((state) => ({" src

2Fix

Select each field with its own useSelector call, or pass shallowEqual as the equality function if the object really must be built in the selector.

import { shallowEqual, useSelector } from 'react-redux';

// option 1: one call per field
const count = useSelector((state) => state.count);
const user = useSelector((state) => state.user);

// option 2: keep the object, compare its fields
const data = useSelector(
  (state) => ({ count: state.count, user: state.user }),
  shallowEqual
);

3Verify

Dispatch an unrelated action. The component no longer shows up in the Profiler for that commit.

store.dispatch({ type: 'unrelated/ping' });

Confirmed by redux.js.org

Did this fix it?

Cause 2 of 4

Derived data (filter, map, sort) is computed inside the selector

1Check

A filtered array is a new array even when every item is the same. The Redux style guide recommends memoized selectors for exactly this case.

grep -rn "useSelector.*\.\(filter\|map\|sort\)(" src

2Fix

Create the selector once, outside the component, with createSelector. It recomputes only when its input selectors return new values.

import { createSelector } from '@reduxjs/toolkit';

const selectTodos = (state) => state.todos.list;
const selectFilter = (state) => state.todos.filter;

export const selectVisibleTodos = createSelector(
  [selectTodos, selectFilter],
  (todos, filter) => todos.filter((t) => t.status === filter)
);

// in the component
const visible = useSelector(selectVisibleTodos);

3Verify

Dispatch an action that changes another slice. The component does not render again.

console.count('TodoList render');

Confirmed by redux.js.org

Did this fix it?

Cause 3 of 4

The selector returns the whole state, or a large slice

1Check

The React Redux docs describe an identity-function check in development builds: it warns when a selector returns the entire root state, because the component then re-renders on any state change.

grep -rn "useSelector((state) => state)" src

2Fix

Select only the values the component displays, using narrow selectors. The Redux style guide advises many small useSelector calls over one large one.

const user = useSelector(selectCurrentUser);
const status = useSelector(selectUserStatus);

3Verify

The development warning about returning the root state is gone, and renders drop to the actions that touch those values.

console.count('Profile render');

Confirmed by redux.js.org

Did this fix it?

Cause 4 of 4

A reducer returns new references for data that did not change

1Check

Redux state must be updated immutably, but only the parts that change should get new references. A reducer that rebuilds the whole slice for every action breaks the === check for every reader.

// look for reducers that always spread or recreate the slice
grep -rn "return {" src/features

2Fix

Use createSlice, which is built on Immer: the Redux style guide notes that Immer tracks your changes and produces immutably updated values, so the parts you did not touch keep their references.

const todosSlice = createSlice({
  name: 'todos',
  initialState,
  reducers: {
    todoToggled(state, action) {
      const todo = state.byId[action.payload];
      if (todo) todo.completed = !todo.completed;
    },
  },
});

3Verify

Log the selected value before and after an unrelated action; the reference comparison is now true.

console.log(store.getState().todos === before);

Confirmed by redux.js.org

Did this fix it?

How do I stop it coming back?

  • Define createSelector selectors outside components so every render shares one instance.
  • Write many narrow useSelector calls instead of one that builds an object.
  • Keep the development checks on; they warn about unstable selectors and root-state selectors.
  • Profile with the React Profiler before optimising: only fix components that render visibly often.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.