为什么上面写着'next'在redux中间件代码中没有定义.中间件的next方法是否已弃用

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

本文关键字:next 中间件 定义 方法 是否 redux 代码 为什么      更新时间:2023-09-26

我得到这个错误:

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

是下一个方法使用的中间件在ReactJS弃用或我使用它不正确?

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

这不起作用,因为您将logger()的返回值传递给applyMiddleware而没有参数。

要修复它,您必须传递logger而不是logger()applyMiddleware()

您可以在这里阅读更多关于自定义中间件的信息

在我的例子中,我试图传递一个增强器作为中间件。将增强器从middleware移动到enhancers修复了这个问题。https://redux-toolkit.js.org/api/configureStore#enhancers