Search code examples
javascriptreactjsreduxreact-redux

Why does it says that 'next' is not defined in redux middleware code. Is next method of middleware deprecated?


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});

Solution

  • 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