Getting TypeError: middleware is not a function

83 Views Asked by At

I am learning Redux, so wrote below logic by following online resources, but it throws this error:

/home/sssawant/Sarvesh/notes_fullstack/frontend/React_notes/redux_demo/node_modules/redux/dist/cjs/redux.cjs:407
const chain = middlewares.map((middleware) => middleware(middlewareAPI));

TypeError: middleware is not a function

at /home/sssawant/Sarvesh/notes_fullstack/frontend/React_notes/redux_demo/node_modules/redux/dist/cjs/redux.cjs:407:51
at Array.map ()
at /home/sssawant/Sarvesh/notes_fullstack/frontend/React_notes/redux_demo/node_modules/redux/dist/cjs/redux.cjs:407:31
at createStore (/home/sssawant/Sarvesh/notes_fullstack/frontend/React_notes/redux_demo/node_modules/redux/dist/cjs/redux.cjs:133:33)
at Object. (/home/sssawant/Sarvesh/notes_fullstack/frontend/React_notes/redux_demo/asyncActions2.js:79:15)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)

Node.js v20.11.0

I have below versions for dependencies.

Node version - v20.11.0

Current dependencies

├── [email protected]
[email protected]
[email protected]
[email protected]

My code:

const redux = require("redux");
const { thunk } = require("redux-thunk");
const createStore = redux.createStore;
const applyMiddleware = redux.applyMiddleware;
const thunkMiddleware = require("redux-thunk").default
const axios = require("axios");

const initialState = {
  loading: false,
  users: [],
  error: "",
};

const FETCH_USERS_REQUEST = "FETCH_USERS_REQUEST";
const FETCH_USERS_SUCCESS = "FETCH_USERS_SUCCESS";
const FETCH_USERS_FAILURE = "FETCH_USERS_FAILURE";

const fetchUsersRequest = () => {
  return {
    type: FETCH_USERS_REQUEST,
  };
};

const fetchUsersSuccess = (users) => {
  return {
    type: FETCH_USERS_SUCCESS,
    payload: users,
  };
};

const fetchUsersFailure = (error) => {
  return {
    type: FETCH_USERS_FAILURE,
    payload: error,
  };
};

const reducer = (curState, action) => {
  switch (action.type) {
    case FETCH_USERS_REQUEST:
      return {
        ...curState,
        loading: true,
      };

    case FETCH_USERS_SUCCESS:
      return {
        ...curState,
        loading: false,
        users: action.payload,
        error: "",
      };

    case FETCH_USERS_FAILURE:
      return {
        ...curState,
        loading: false,
        users: [],
        error: action.payload,
      };
  }
};

const fetchUsers = () => {
  return function (dispatch) {
    dispatch(fetchUsersRequest());
    axios
      .get("https://jsonplaceholder.typicode.com/users")
      .then((rsponse) => {
        const users = response.data.map((user) => user.id);
        dispatch(fetchUsersSuccess(users));
      })
      .catch((error) => {
        dispatch(fetchUsersFailure(error.message));
      });
  };
};

const store = createStore(reducer, applyMiddleware(thunkMiddleware));
store.subscribe(() => {console.log(store.getState())});
store.dispatch(fetchUsers())

Why am I getting this error? Is it a syntactical error, or due to different versions for Node and Redux-Thunk?

1

There are 1 best solutions below

0
Drew Reese On

In readux-thunk@3 there is no longer any default export, so require("redux-thunk").default is undefined.

Compare v2

...

const thunk = createThunkMiddleware() as ThunkMiddleware & {
  withExtraArgument<
    ExtraThunkArg,
    State = any,
    BasicAction extends Action = AnyAction
  >(
    extraArgument: ExtraThunkArg
  ): ThunkMiddleware<State, BasicAction, ExtraThunkArg>
}

// Attach the factory function so users can create a customized version
// with whatever "extra arg" they want to inject into their thunks
thunk.withExtraArgument = createThunkMiddleware

export default thunk

to v3

...

function createThunkMiddleware<
  State = any,
  BasicAction extends Action = AnyAction,
  ExtraThunkArg = undefined
>(extraArgument?: ExtraThunkArg) {
  // Standard Redux middleware definition pattern:
  // See: https://redux.js.org/tutorials/fundamentals/part-4-store#writing-custom-middleware
  const middleware: ThunkMiddleware<State, BasicAction, ExtraThunkArg> =
    ({ dispatch, getState }) =>
    next =>
    action => {
      // The thunk middleware looks for any functions that were passed to `store.dispatch`.
      // If this "action" is really a function, call it and return the result.
      if (typeof action === 'function') {
        // Inject the store's `dispatch` and `getState` methods, as well as any "extra arg"
        return action(dispatch, getState, extraArgument)
      }

      // Otherwise, pass the action down the middleware chain as usual
      return next(action)
    }
  return middleware
}

export const thunk = createThunkMiddleware()

...

Update your code to use thunk instead of the undefined thunkMiddleware reference.

const redux = require("redux");
const { thunk } = require("redux-thunk"); // <-- the middleware
const createStore = redux.createStore;
const applyMiddleware = redux.applyMiddleware;
const axios = require("axios");

...

const store = createStore(
  reducer,
  applyMiddleware(thunk) // <-- pass thunk middleware
);