const createStore = (reducer)=>{ //默认的state对象 let state = {}; //将所有订阅的事件存在在这个数组中 let listeners = []; //默认的action let actionTypes = "@@redux/INIT"; let Initaction = { type:actionTypes } const dispatch = (action=Initaction)=>{ state = reducer(state,action); listeners.map(cb=>{ cb(); }) } dispatch(); const getState = ()=>state; const subscribe = (cb)=>{ listeners.push(cb); } return { dispatch, getState, subscribe } } const combineReducers = (reducers)=>{ const newState = {}; return function(state,action){ for(let key in reducers){ let reduce = reducers[key]; console.log(newState[key]) newState[key] = reduce(state[key],action); } return newState; } } export {createStore,combineReducers}
原文:https://www.cnblogs.com/Leslie-Cheung1584304774/p/10734672.html