javascript 为什么它说'next'没有在redux中间件代码中定义,中间件的next方法被弃用了吗?

j1dl9f46  于 2023-03-11  发布在  Java
关注(0)|答案(2)|浏览(128)

我收到此错误:
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});
yi0zb3m4

yi0zb3m41#

这是不起作用的,因为您传递给applyMiddleware的是logger()的返回值,没有参数。
要修复它,必须将logger而不是logger()传递给applyMiddleware()
您可以阅读有关定制中间件here的更多信息

y0u0uwnf

y0u0uwnf2#

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

相关问题