react中createStore, conbineRdeducers的简易封装

Posted wildccy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了react中createStore, conbineRdeducers的简易封装相关的知识,希望对你有一定的参考价值。

const createStore = (reducer)=>{
        let state = {};//默认的state对象
        let lists = [];//存贮订阅事件
        let actiontypes = "@@init" //默认的type
        let initaction = {
            type :actiontypes //默认的action
        }

        const dispatch = (action = initaction)=>{
            state = reducer(state,action)//将action给了store,store给了reducer 最后reducer修改数据返回state
            lists.map(cb=>{
                cb();//通知组件更新
            })
        }

        const getState = ()=>state //将state return出去

        const subscribe = (cb)=>{
            lists.push(cb)//订阅事件(数据更新)
        }

        dispatch();//初始化


        return {
            dispatch,
            getState,
            subscribe
        }

    }

    const conbineRdeducers = (reducers)=>{
        const newState = {};
        //{tab:{},num:{}}
        return function (state,action) { //reducers为一个函数 所以要ruturn出去一个函数
            for(let key in reducers){//通过遍历将key值放入newState中
                newState[key] = reducers[key](state[key],action)
            }
            return newState
        }
    }

    export {
        createStore,
        conbineRdeducers
    }

 

以上是关于react中createStore, conbineRdeducers的简易封装的主要内容,如果未能解决你的问题,请参考以下文章

react中redux的理解

看起来您正在将几个商店增强器传递给 createStore() react-thunk

React中Redux的有关知识

Redux createStore/applyMiddleWare Q

我可以在组件中使用redux中的createStore()吗?

react_3/redux