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

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


reduxImmutableStateInvariant is a function that you need to call before passing it into 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 this in the docs?

In the github README docs, is called after being imported (via require) reduxImmutableStateInvariant. See the third line, below:

// 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.0const store = createStore(  reducer,  applyMiddleware(...middleware));

Why isn't thunk a function, though?

In the thunk middleware, the thunk function is called before it is returned.

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

So why is redux-immutable-state-invariant a function?

Based on the code, it looks like you can pass in a function (isImmutable), that is used to determine which properties in your redux state are immutable. I think that providing your own isImmutable function is what allows this middleware to work nicely with other immutable libraries.

export default function immutableStateInvariantMiddleware(isImmutable = isImmutableDefault) {

That method is used herehttps://github.com/leoasis/redux-immutable-state-invariant/blob/5ed542246e32b7eec06879b25e5a0a478daf4892/src/trackForMutations.js#L5