首页 > 其他 > 详细

react中对于redux的封装

时间:2019-04-19 11:44:38      阅读:253      评论:0      收藏:0      [点我收藏+]
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}

 

react中对于redux的封装

原文:https://www.cnblogs.com/Leslie-Cheung1584304774/p/10734672.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!