Redux之源码分析(九)
# 一、index.js
- 暴露了几个核心
API
import createStore from './createStore';
import combineReducers from './utils/combineReducers';
import bindActionCreators from './utils/bindActionCreators';
import applyMiddleware from './utils/applyMiddleware';
import compose from './utils/compose';
export {
createStore,
combineReducers,
bindActionCreators,
applyMiddleware,
compose
};
@前端进阶之旅: 代码已经复制到剪贴板
# 二、createStore.js
https://github.com/reactjs/redux/blob/master/src/createStore.js
redux.createStore(reducer, initialState)传入了reducer、initialState,并返回一个store对象store对象对外暴露了dispatch、getState、subscribe方法store对象通过getState()获取内部状态initialState为store的初始状态,如果不传则为undefinedstore对象通过reducer来修改内部状态store对象创建的时候,内部会主动调用dispatch({ type: ActionTypes.INIT });来对内部状态进行初始化。通过断点或者日志打印就可以看到,store对象创建的同时,reducer就会被调用进行初始化
import isPlainObject from './utils/isPlainObject';
/**
* These are private action types reserved by Redux.
* For any unknown actions, you must return the current state.
* If the current state is undefined, you must return the initial state.
* Do not reference these action types directly in your code.
*/
// 初始化的时候(redux.createStore(reducer, initialState)时),传的action.type 就是这货啦
export var ActionTypes = {
INIT: '@@redux/INIT'
};
/**
* Creates a Redux store that holds the state tree.
* The only way to change the data in the store is to call `dispatch()` on it.
*
* There should only be a single store in your app. To specify how different
* parts of the state tree respond to actions, you may combine several reducers
* into a single redu