combineReucr

'use strict';
const Redux = require('redux');
const createStore = Redux.createStore;
const combineReducers = Redux.combineReducers;

// const reducers = {}
// const reducer = combineReducers(reducers);
// const reducers ={a:function(state,action){},b:function(state,action){}}
function aReducer(state,action){
    if(typeof state === 'undefined') return [];
    switch(action.type){
        case 'a':
            return state.concat([action.data]);
        default:
            return state;
    }
}
function bReducer(state,action){
    if(typeof state === 'undefined') return [];
    switch(action.type){
        case 'b':
            return state.concat([action.data]);
        default:
            return state;
    }
}
function cNameReducer(state,action){
    if(typeof state === 'undefined') return '';
    if(action.type === 'c'){
        return action.name;
    }else{
        return state;
    }
}
function cGroupReducer(state,action){
    if(typeof state === 'undefined') return [];
    if(action.type === 'c'){
        return state.concat(action.item);
    }else{
        return state;
    }
}
//cAction:{type,name,item}
function cReducer(state,action){
    if(typeof state === 'undefined') return {name:'',group:[]};
    switch(action.type){
        case 'c':
                // name:cNameReducer(undefined,action),
                // group:cGroupReducer(undefined,action),
                return combineReducers({name:cNameReducer,group:cGroupReducer})(state,action);

        default:
            return state;
    }
}
const reducers = combineReducers({a:aReducer,b:bReducer,c:cReducer});
const store = createStore(reducers,{a:[111],b:[222],c:{name:'',group:[]}});

store.subscribe(function listener(){
    console.log(store.getState());
})
let actionA = {
    type:'a',
    data:'leo'
}
store.dispatch(actionA);
let actionB = {
    type:'b',
    data:'wan'
}
store.dispatch(actionB);
let actionC = {
    type:'c',
    name:'brightas',
    item:'123'
}
store.dispatch(actionC);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值