react中对于redux的封装

时间:2024-01-21 17:25:09
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}