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?
In
readux-thunk@3there is no longer anydefaultexport, sorequire("redux-thunk").defaultis undefined.Compare v2
to v3
Update your code to use
thunkinstead of the undefinedthunkMiddlewarereference.