2

I am getting this error:

index.js?dadc:6Uncaught TypeError: next is not a function

Is the next method used by middleware in ReactJS deprecated or have I used it incorrectly?

import { applyMiddleware, combineReducers , createStore } from 'redux';

const logger =(store) => (next) => (action) => {

    console.log('middle-ware called');
    next(action);

}

const reducer=(state ,action)=>{
    if(action.type=='INC'){
        console.log('a-----------------',action);
        return state+action.payload;
    }
    else
        return state; };

const store=createStore(reducer ,1 ,applyMiddleware(logger()));



store.subscribe(()=>{
    console.log('store changed',store.getState()) });

store.dispatch({type :'INC' ,payload : 10}); store.dispatch({type :'INC' ,payload : 10});
Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
Vishnu Shekhawat
  • 1,245
  • 1
  • 10
  • 20

2 Answers2

5

That's not working because you are passing to applyMiddleware the returned value of logger() with no arguments.

To fix it, you have to pass logger instead of logger() to applyMiddleware().

You can read more about custom middlewares here

QoP
  • 27,388
  • 16
  • 74
  • 74
  • I solved my post https://stackoverflow.com/questions/72978999/redux-middleware-with-configurestore-next-is-not-a-function with your soution. Thank you – sineverba Jul 18 '22 at 07:46
0

In my case, I was trying to pass an enhancer as middleware. Moving the enhancer from middleware to enhancers fixed the problem.https://redux-toolkit.js.org/api/configureStore#enhancers

ItsGeorge
  • 2,060
  • 3
  • 17
  • 33