score:1

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 redux@3.7.2

score:1

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

score:2

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))
);
...

score:2

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.

score:2

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

score:3

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.

score:5

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

score:5

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.

score:5

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),

score:11

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.

score:24

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);

score:195

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)

  2. 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.

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.


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


Related Query

More Query from same tag