我收到此错误: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});
2条答案
按热度按时间yi0zb3m41#
这是不起作用的,因为您传递给
applyMiddleware
的是logger()
的返回值,没有参数。要修复它,必须将
logger
而不是logger()
传递给applyMiddleware()
。您可以阅读有关定制中间件here的更多信息
y0u0uwnf2#
在我的例子中,我尝试将增强器作为中间件传递,将增强器从
middleware
移动到enhancers
修复了problem.https://redux-toolkit.js.org/api/configureStore#enhancers