Redux Async Solution Redux-Thunk
发布于 2026-07-06 10:47:33
2 次浏览
function createThunkMiddleware(extraArgument) {
return function thunk(store) {
return function (next) {
return function (action) {
// Destructure dispatch, getState from store
const { dispatch, getState } = store;
// If action is function, call it with dispatch and getState
if (typeof action === 'function') {
return action(dispatch, getState, extraArgument); // extraArgument can be passed here
}
// Otherwise treat as normal action
let result = next(action);
return result
}
}
}
}
const thunk = createThunkMiddleware();
thunk.withExtraArgument = createThunkMiddleware;
export default thunk;
Explanation: https://segmentfault.com/a/1190000037437347
暂无评论。