reduxjs/redux · error · Error
You may not call store.getState() while the reducer is execu
Error message
You may not call store.getState() while the reducer is executing. The reducer has already received the state as an argument. Pass it down from the top reducer instead of reading it from the store.
What it means
store.getState() reads currentState, but while a reducer is executing (isDispatching === true) the state is mid-update and inconsistent. Reading it from inside a reducer would observe a half-computed tree and could break reducer purity. Redux guards getState with the isDispatching flag so reducers must rely only on the state argument handed to them.
Source
Thrown at src/createStore.ts:168
* subscribe/unsubscribe in the middle of a dispatch.
*/
function ensureCanMutateNextListeners() {
if (nextListeners === currentListeners) {
nextListeners = new Map()
currentListeners.forEach((listener, key) => {
nextListeners.set(key, listener)
})
}
}
/**
* Reads the state tree managed by the store.
*
* @returns The current state tree of your application.
*/
function getState(): S {
if (isDispatching) {
throw new Error(
'You may not call store.getState() while the reducer is executing. ' +
'The reducer has already received the state as an argument. ' +
'Pass it down from the top reducer instead of reading it from the store.'
)
}
return currentState as S
}
/**
* Adds a change listener. It will be called any time an action is dispatched,
* and some part of the state tree may potentially have changed. You may then
* call `getState()` to read the current state tree inside the callback.
*
* You may call `dispatch()` from a change listener, with the following
* caveats:
*
* 1. The subscriptions are snapshotted just before every `dispatch()` call.View on GitHub (pinned to 3084fc33bb)
Solutions
- Do not call store.getState() inside a reducer. Use the state argument passed to the reducer, or lift the cross-slice read into middleware/thunk that runs outside the dispatch.
- If you need data from another slice, structure the action payload to carry it, or use redux-thunk to read getState() before dispatching.
- Move logging/devtools reads to a subscribe() callback, which fires after the dispatch completes.
Example fix
// before
function cart(state = [], action) {
const user = store.getState().user // throws while dispatching
...
}
// after (in a thunk, outside the reducer)
const checkout = () => (dispatch, getState) => {
const { cart, user } = getState()
dispatch({ type: 'CHECKOUT', payload: { cart, user } })
} Defensive patterns
Strategy: validation
Validate before calling
// Forbidden inside reducers. Instead, read cross-slice state in a thunk:
const thunk = () => (dispatch, getState) => {
const other = getState().otherSlice // OK: outside dispatch window
dispatch({ type: 'X', payload: other })
} Type guard
// Reducer signature should never reference the store. Static rule. // ESLint: 'no-restricted-globals' or custom rule to ban store.getState in reducer files.
Prevention
- Never call store.getState() inside a reducer; use the state argument.
- Cross-slice reads belong in middleware/thunk (getState is safe there).
- Review reducers for any external store reference.
When it happens
Trigger: A reducer that calls store.getState() (e.g. to read another slice); middleware that calls api.getState() during the next(action) call while the reducer is running; a selector invoked synchronously inside a reducer; devtools or logging code that pulls state during dispatch.
Common situations: Cross-slice reads where a reducer wants data from another slice (should be done via thunk or via the action payload); migrated code that used a global store reference inside reducers; selector recomputation triggered during dispatch; SSR initial-dispatch loop reading state mid-update.
Related errors
- Reducers may not dispatch actions.
- Expected the root reducer to be a function. Instead, receive
- You may not call store.subscribe() while the reducer is exec
- You may not unsubscribe from a store listener while the redu
- The slice reducer for key "${key}" returned undefined during
AI-assisted analysis of reduxjs/redux@3084fc33bb (2026-08-12).
Data as JSON: /api/errors/d9572ef35d608ac8.
Report an issue: GitHub.