以这种方式重用reducer是否是redux中的有效模式

时间:2019-08-02 07:12:37

标签: redux

我正在问以这种方式重用redux逻辑是否是有效的模式 通过在操作和化简器中添加id,以便化简器可以识别其相应的操作

    // the heighre order reducer
    const counterReducer = (id, initialState) => (state = initialState, action) => {

         // check for the id of the reducer is the same as in the action meta data
          if (action && action.id === id) {
             // continue to reducer

            switch (action.type) {
              case "INC":
                return state+1
              default:
                return state;
            }
          } else {
            // return the initial state
            return state;
          }
        };
    const store = createStore(combineReducers({
      c1:counterReducer("c1",0),
      c2:counterReducer("c2",1),
      c3:counterReducer("c3",2)
    }));

    // dispatch action to first reducer
    store.dispatch({type:INC,meta:{id:"c1"}});

    // dispatch action to second reducer
     store.dispatch({type:INC,meta:{id:"c2"}});

    // dispatch action to second reducer
    store.dispatch({type:INC,meta:{id:"c3"}});

0 个答案:

没有答案