Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Using this code in my configureStore.dev.js file, I get an Uncaught TypeError: getState is not a function when adding applyMiddleware(reduxImmutableStateInvariant). When I remove this added middleware, my project runs fine. What is the proper 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;
}
like image 249
kibowki Avatar asked Aug 19 '16 21:08

kibowki


1 Answers

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.0
const 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 here https://github.com/leoasis/redux-immutable-state-invariant/blob/5ed542246e32b7eec06879b25e5a0a478daf4892/src/trackForMutations.js#L5

like image 144
Cory Danielson Avatar answered Oct 19 '22 20:10

Cory Danielson