"Error: You may not call store.getState() while the reducer is executing."

JavascriptReactjsReduxReact Redux

Javascript Problem Overview


I just upgraded my fully functional react-native app to Redux v4, but now I am getting the following error:

> Error: Error: Error: 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.

I suspect the problem is that I have numerous components inside others, each with their own connect(mapStateToProps, mapDispatchToProps)(Component) and I suppose this is not the correct way to implement it, though I am not sure the proper way to go about it.

Any direction is greatly appreciated!

Stack Trace:

    This error is located at:
    in Connect(SideBarApp) (at SceneView.js:9)
    in SceneView (at createTabNavigator.js:10)
    in RCTView (at View.js:43)
    in RCTView (at View.js:43)
    in ResourceSavingScene (at createBottomTabNavigator.js:86)
    in RCTView (at View.js:43)
    in RCTView (at View.js:43)
    in TabNavigationView (at createTabNavigator.js:127)
    in NavigationView (at createNavigator.js:59)
    in Navigator (at createNavigationContainer.js:376)
    in NavigationContainer (at SceneView.js:9)
    in SceneView (at SwitchView.js:12)
    in SwitchView (at createNavigator.js:59)
    in Navigator (at createNavigationContainer.js:376)
    in NavigationContainer (at AppNavigator.js:36)
    in App (created by Connect(App))
    in Connect(App) (at index.ios.js:23)
    in Provider (at index.ios.js:22)
    in TheNewsApp (at renderApplication.js:32)
    in RCTView (at View.js:43)
    in RCTView (at View.js:43)
    in AppContainer (at renderApplication.js:31)

This error is located at:
    in NavigationContainer (at SceneView.js:9)
    in SceneView (at SwitchView.js:12)
    in SwitchView (at createNavigator.js:59)
    in Navigator (at createNavigationContainer.js:376)
    in NavigationContainer (at AppNavigator.js:36)
    in App (created by Connect(App))
    in Connect(App) (at index.ios.js:23)
    in Provider (at index.ios.js:22)
    in TheNewsApp (at renderApplication.js:32)
    in RCTView (at View.js:43)
    in RCTView (at View.js:43)
    in AppContainer (at renderApplication.js:31)

This error is located at:
    in NavigationContainer (at AppNavigator.js:36)
    in App (created by Connect(App))
    in Connect(App) (at index.ios.js:23)
    in Provider (at index.ios.js:22)
    in TheNewsApp (at renderApplication.js:32)
    in RCTView (at View.js:43)
    in RCTView (at View.js:43)
    in AppContainer (at renderApplication.js:31)
getState@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:79579:24
runComponentSelector@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:78896:56
initSelector@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:79019:28
Connect(SideBarApp)@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:78969:29
constructClassInstance@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:20826:32
updateClassComponent@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:22393:35
performUnitOfWork@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:24922:27
workLoop@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:24955:47
renderRoot@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:24988:21
performWorkOnRoot@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:25549:23
performWork@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:25481:30
performSyncWork@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:25456:20
requestWork@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:25362:26
scheduleWork@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:25224:28
enqueueSetState@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:20681:23
setState@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:2380:37
dispatch@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:80901:27
navigate@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:80517:24
nav@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:80470:44
combination@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:79810:38
dispatch@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:79628:38
setLoginStatus@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:112247:19
http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:112199:44
tryCallOne@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:8818:16
http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:8919:27
_callTimer@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:8162:17
_callImmediatesPass@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:8198:19
callImmediates@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:8417:33
__callImmediates@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7741:32
http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7580:34
__guard@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7721:15
flushedQueue@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7579:21
flushedQueue@[native code]
invokeCallbackAndReturnFlushedQueue@[native code]

Javascript Solutions


Solution 1 - Javascript

It's an issue with the new v2.16.0 release of redux-devtools-extension.

Here's a couple of workaround while we wait for the fix...

  1. Revert back to the working version v2.15.5 (For Chrome)
  1. Or just simply disable your redux-devtool extension for now.
  • either on the browser level or through your code(where you create the redux store)


FYI: this does not solve the OP's question but does solve the issue where developers are receiving the below error message starting 11/27/18.


UPDATE

v2.16.2 has been released

For those who had previously disabled the extension, just re-enable it and Update your redux dev tools from 2.16.0 to 2.16.2 Update Redux Dev tools

Solution 2 - Javascript

In my case, I have to remove composeWithDevTools - a plugin for chrome

import { createStore, combineReducers, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import thunk from 'redux-thunk';

// const enhancer = composeWithDevTools(applyMiddleware(thunk))
const enhancer = applyMiddleware(thunk)

const store = createStore(reducers, enhancer);

Solution 3 - Javascript

In my project. This issue just popup from nowhere one day.

My solution: Disable the Chrome extension - Redux Devtools. Then everything is back to normal.

So with this kind of error, you should test in several browser to find the problem.

Solution 4 - Javascript

Disabling chrome extension or removing composeWithDevTool from your code will work as a quick fix. But we all know that we need the extension in order to track our application state and manage it properly. So I created an issue today please support hopefully, someone from the redux team will get back to us.

Or if you are looking for a temporary workaround(for chrome), you can download https://github.com/zalmoxisus/redux-devtools-extension/releases/download/2.15.5/extension.zip and then extract it to some folder.

Type chrome://extensions and turn on developer mode from top left and then click on Load Unpacked and select the extracted folder for use.

Issue: https://github.com/reduxjs/redux-devtools/issues/413

Solution 5 - Javascript

Solution That work for me

Disable the Chrome extension of Redux Dev tools. or remove logger from your code.

update: Update your redux dev tools from 2.16.0 to 2.16.1 Update Redux Dev tools.

Solution 6 - Javascript

tl;dr

Make sure you don't have any code that causes side effects in your reducers!

Pure reducers

Redux reducers need to be pure. This means they shouldn't have side effects. Side effects should go to thunks or sagas. In my case a reducer looked like this:

case REDIRECT_ON_EVENT: {
  history.push('/some-route'); // side effect: redirection
  return {
    ...state,
    path: action.payload.path,
  };
}

The history.push('/some-route'); part messed up state management. Removing it from the reducer and placing it to a saga that gets called on the same action type fixed the issue:

function redirectToSomeRoute() {
  history.push('/some-route');
}

takeEvery(REDIRECT_ON_EVENT, redirectToSomeRoute),

Solution 7 - Javascript

In my case the issue was not related to dev extension. I've encountered this error while working with deep-linking - was updating route query inside reducer. Wrapping logic related to manipulating url inside settimeout fixed the error.

Solution 8 - Javascript

I had the same error with a PWA build with polymer 3. The store.js also tried to use the redux dev tools which I had to deactivate:

...
// Sets up a Chrome extension for time travel debugging.
// See https://github.com/zalmoxisus/redux-devtools-extension for more information.
//const devCompose = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const devCompose =  compose;

// Initializes the Redux store with a lazyReducerEnhancer (so that you can
// lazily add reducers after the store has been created) and redux-thunk (so
// that you can dispatch async actions). See the "Redux and state management"
// section of the wiki for more details:
// https://github.com/Polymer/pwa-starter-kit/wiki/4.-Redux-and-state-management
export const store = createStore(
  state => state,
  devCompose(
    lazyReducerEnhancer(combineReducers),
    applyMiddleware(thunk))
);
...

Solution 9 - Javascript

This is what I did: Just commented the line for Chrome Redux Devtools Extension from my store.js file.

....

 const store = createStore(
  rootReducer,
  initialState,
  compose(
    applyMiddleware(...middleware)
   ///This line--> window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() 
  )
);

....

And the issue is just begun an hour ago. As we all know the extension is very handy during development, so let us wait for the real fix from the authorities.

Solution 10 - Javascript

Should be fixed now. Update your redux dev tools from 2.16.0 to 2.16.1

https://github.com/zalmoxisus/redux-devtools-extension/issues/588#issuecomment-442396540

Solution 11 - Javascript

I had this problem on Chrome. Downgrading my version of redux from 4.0.2 to 3.7.2 fixed it for me.

npm uninstall redux

npm install [email protected]

Solution 12 - Javascript

> Note: I am using Saga

In my case I had added a navigation code inside a reducer.

    sendOTPSuccess: (state, action) => {
      state.wip = false;

      RootNavigation.navigate('LoginOTPScreen');
    },

In the beginning it was working but when I used/accessed state inside the called new Screen

import { useDispatch, useSelector } from "react-redux";
import * as sessionActions from "../../models/session";

const LoginOTP: () => Node = (props) => {
  const session = useSelector(sessionActions.selectSession);
  const dispatch = useDispatch();
  ...

the error popped up.

Error: You may not call store.getState() while the reducer is executing.”

Cause

Reducer is supposed to be a pure function. It should accept a payload and modify state and nothing else. Something else is considered as a side-effect.

I my case I loaded a new component which also consumes state data and the state modification is not yet completed. This is causing the issue.

Solution

I simply moved the navigation to respective saga

// worker Saga:
function* sendOTPSaga({ payload }) {
  const { phoneNumber } = payload;
  try {
    const response = yield call(Api.sendOtp, { phoneNumber });
    if (response.status == 200) {
      yield put(sessionActions.sendOTPSuccess(response.data));
      RootNavigation.navigate('LoginOTPScreen'); // <---- Here
    ...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbbrodskyView Question on Stackoverflow
Solution 1 - JavascriptVictor LeView Answer on Stackoverflow
Solution 2 - JavascriptCong Dan LuongView Answer on Stackoverflow
Solution 3 - JavascriptVan Hoang TruongView Answer on Stackoverflow
Solution 4 - JavascriptAlqama Bin SadiqView Answer on Stackoverflow
Solution 5 - JavascriptSultan AslamView Answer on Stackoverflow
Solution 6 - JavascripttotymedliView Answer on Stackoverflow
Solution 7 - JavascriptegoView Answer on Stackoverflow
Solution 8 - JavascriptPatrick CornelissenView Answer on Stackoverflow
Solution 9 - JavascriptFawaz AbdullaView Answer on Stackoverflow
Solution 10 - JavascriptandrevenancioView Answer on Stackoverflow
Solution 11 - JavascriptQuintin SheridanView Answer on Stackoverflow
Solution 12 - JavascriptillusionistView Answer on Stackoverflow