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

twilco

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;
}
Cory Danielson

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TypeError: undefined is not a function when unit-testing custom redux hook

Redux: Calling store.getState() in a reducer function, is that an anti pattern?

getState in redux-saga?

TypeError: middleware is not a function

Uncaught TypeError: store.getState is not a function

redux-promise: Uncaught TypeError: middleware is not a function

TypeError: store.getState is not a function in react-redux

TypeError: Object(...) is not a function (redux)

Redux action return type when using dispatch and getState

React-Redux, when dispatching a function, "uncaught TypeError is not a function"

react-redux : TypeError: store.getState is not a function

React Native TypeError store.getState is not a function

Redux Testing - getState() TypeError: Cannot read property 'startDateIndex' of undefined

× TypeError: middleware is not a function

TypeError: middleware is not a function in store

store.getState is not a function Redux-persist

TypeError: getState is not a function in redux when trying to pass token

TypeError: searchField.toLowerCase is not a function when using hooks an redux

TypeError: store.getState is not a function. (In 'store.getState()', 'store.getState' is undefined how can i resolve this problem?

TypeError: _this.store.getState is not a function when using connect from Redux

Function inside of Redux middleware not working

TypeError: store.getState is not a function. (In 'store.getState()', 'store.getState' is undefined

state.getState() is not a function while updating to redux 4

I am getting this error: TypeError: store.getState is not a function

"Cannot read property 'getState' of undefined" when creating redux mock store

TypeError: store.getState is not a function (redux)

Redux/toolkit TypeError: Cannot read properties of undefined (reading 'getState')

TypeError: Cannot read property ‘getState’ of undefined in Redux Tookit

redux tookit when using a middleware builder function, an array of middleware must be returned

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive