We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
下面是redux的applyMiddleware代码
export default function applyMiddleware( ...middlewares: Middleware[] ): StoreEnhancer<any> { return (createStore: StoreEnhancerStoreCreator) => <S, A extends AnyAction>( reducer: Reducer<S, A>, preloadedState?: PreloadedState<S> ) => { const store = createStore(reducer, preloadedState) let dispatch: Dispatch = () => { throw new Error( 'Dispatching while constructing your middleware is not allowed. ' + 'Other middleware would not be applied to this dispatch.' ) } const middlewareAPI: MiddlewareAPI = { getState: store.getState, dispatch: (action, ...args) => dispatch(action, ...args) } const chain = middlewares.map(middleware => middleware(middlewareAPI)) dispatch = compose<typeof dispatch>(...chain)(store.dispatch) return { ...store, dispatch } } }
下面就是Thunk这个中间件的代码
function createThunkMiddleware(extraArgument) { return ({ dispatch, getState }) => (next) => (action) => { if (typeof action === 'function') { return action(dispatch, getState, extraArgument); } return next(action); }; } const thunk = createThunkMiddleware(); thunk.withExtraArgument = createThunkMiddleware; export default thunk;
这个中间件返回了嵌套的函数闭包,其中下面的函数就是applyMiddlewares的 store.dispatch
store.dispatch
(action) => { if (typeof action === 'function') { return action(dispatch, getState, extraArgument); } return next(action); }; }
回忆一下,我们 connect 之后的组件通过 dispatch 调用,传递的
connect
dispatch
{ type: SET_MESSAGE, payload: 'hello' }
就是 action 的参数结构
action
The text was updated successfully, but these errors were encountered:
No branches or pull requests
下面是redux的applyMiddleware代码
下面就是Thunk这个中间件的代码
这个中间件返回了嵌套的函数闭包,其中下面的函数就是applyMiddlewares的
store.dispatch
回忆一下,我们
connect
之后的组件通过dispatch
调用,传递的就是
action
的参数结构The text was updated successfully, but these errors were encountered: