TypeError: getState is not a function when adding middleware to Redux

Using this code in the configureStore.dev.js file, I add Uncaught TypeError: getState is not a functionon add applyMiddleware(reduxImmutableStateInvariant). When I remove this added middleware, my project works fine. What is the correct way to add this middleware? Here is the full file:

import {createStore, compose, applyMiddleware} from 'redux';
import rootReducer from '../reducers';
import reduxImmutableStateInvariant from 'redux-immutable-state-invariant';

export default function configureStore(initialState) {
  const store = createStore(rootReducer, initialState, compose(
    // Add other middleware on this line...
    applyMiddleware(reduxImmutableStateInvariant),
    window.devToolsExtension ? window.devToolsExtension() : f => f // add support for Redux dev tools
    )
  );

  if (module.hot) {
    // Enable Webpack hot module replacement for reducers
    module.hot.accept('../reducers', () => {
      const nextReducer = require('../reducers').default; // eslint-disable-line global-require
      store.replaceReducer(nextReducer);
    });
  }

  return store;
}
+4
source share
1 answer

reduxImmutableStateInvariant is the function that must be called before passing it to applyMiddleware.

const store = createStore(rootReducer, initialState, compose(
        // Add other middleware on this line...
        applyMiddleware(reduxImmutableStateInvariant()),
        window.devToolsExtension ? window.devToolsExtension() : f => f // add support for Redux dev tools
    )
);

Where is it in the docs?

github README docs ( require) reduxImmutableStateInvariant. . :

// Be sure to ONLY add this middleware in development!
const middleware = process.env.NODE_ENV !== 'production' ?
  [require('redux-immutable-state-invariant')(), thunk] :
  [thunk];

// Note passing middleware as the last argument to createStore requires redux@>=3.1.0
const store = createStore(
  reducer,
  applyMiddleware(...middleware)
);

thunk ?

thunk thunk .

const thunk = createThunkMiddleware();
thunk.withExtraArgument = createThunkMiddleware;

export default thunk;

, ---?

, , (isImmutable), , . , isImmutable - , .

export default function immutableStateInvariantMiddleware(isImmutable = isImmutableDefault) {

https://github.com/leoasis/redux-immutable-state-invariant/blob/5ed542246e32b7eec06879b25e5a0a478daf4892/src/trackForMutations.js#L5

+10

Source: https://habr.com/ru/post/1651841/


All Articles