Redux 为什么thunk的参数被包装在一个对象中?
我试图理解redux中的thunks:Redux 为什么thunk的参数被包装在一个对象中?,redux,thunk,Redux,Thunk,我试图理解redux中的thunks: const thunk = ({ dispatch, getState }) => next => action => { if (typeof action === 'function') { return action(dispatch); } return next(action); }; 我很难理解为什么参数被作为对象传递(或者是对象解构?)。为什么不将它们作为dispatch,getState传递?Redu
const thunk = ({ dispatch, getState }) => next => action => {
if (typeof action === 'function') {
return action(dispatch);
}
return next(action);
};
我很难理解为什么参数被作为对象传递(或者是对象解构?)。为什么不将它们作为
dispatch,getState
传递?Redux中间件被提供一个Redux存储API的微型版本作为最外层函数的参数。完整存储API是{dispatch,subscribe,getState,replaceReducer}
。中间件只能访问{dispatch,getState}
至于为什么它是一个对象而不是单独的参数
const middlewareAPI={
getState:store.getState,
调度:(…args)=>调度(…args)
}
const chain=middleware.map(中间件=>middleware(middlewareAPI))
dispatch=组合(…链)(store.dispatch)