@stkevintan
2018-09-23T10:18:15.000000Z
字数 1961
阅读 781
未分类
源码很短,非常精简:
- compose: function reducer
- applymiddleware: createStore的一个enhance函数,对dispatch函数进行层层compose
- middleware: 接收一个旧dispatch,返回一个新的dispatch,在其中处理各种事情
- bindActionCreators: actionCreators.map(fn => (...args) => dispatch(fn(...args)))
- createStore: 对于reducer的getter和setter(dispatch),以及一些subscribe、observable接口等。
- combineReduces: 整合函数
function createThunkMiddleware(extraArgument) {
return ({ dispatch, getState }) => next => action => {
if (typeof action === 'function') { // 使redux能够接收类型为function的action
return action(dispatch, getState, extraArgument);
}
return next(action);
};
}
const thunk = createThunkMiddleware();
thunk.withExtraArgument = createThunkMiddleware;
export default thunk;
export default function promiseMiddleware({ dispatch }) {
return next => action => {
if (!isFSA(action)) {
return isPromise(action)
? action.then(dispatch)
: next(action);
}
return isPromise(action.payload)
? action.payload.then(
result => dispatch({ ...action, payload: result }),
error => {
dispatch({ ...action, payload: error, error: true });
return Promise.reject(error);
}
)
: next(action);
};
}
https://github.com/sunyongjian/blog/issues/21
https://juejin.im/post/59cb5eba5188257e84671aca
// selectorFactory
(dispatch, options) => (state, props) => ({ thing: state.things[props.thingId], saveThing: fields => dispatch(actionCreators.saveThing(props.thingId, fields)),})
其实react-redux主要的职责就是一个,从context里面获取store,然后使用selectorFactory创建一个selector,其中会调用用户提供的mapStateProps,mapDispatchToProps和mergeProps方法,将store的state和dispatch跟组件的ownProps合并。然后还有一个makeSelectorStatefull方法,通过shallowCompare的比较方式来对比当前props和之前的props确定shouldComponentUpdate是否为false
context 可能会因为某个组件shouldComponent而卡住
Redux-saga
2333