2017-04-11 6 views
0

combineReducersとreduceReducersを一緒に使うにはどうすればいいですか?私はいくつかの減速機をコンバインドとして使用したいと思いますが、私はすべての状態を管理する必要がある減速機が必要です(combineReducerのような部分だけでなく)。combineReducersとreduceReducersを一緒に使うにはどうすればいいですか?

const combinedReducers = combineReducers({reducerA}); 
export const reducers= reduceReducers(combinedReducers, reducerB); 

同じ構造の場合: interface State{ reducerAState: reducerA; reducerBState: reducerB; }

をしかし、この場合には、reducerAは以前の状態なしで、唯一自分ですべての状態を上書き 私が使用してみてください。

答えて

0

これは実現できません。 combineReducersによって作られたリデューサは、初期化キーからレデューサーのマップの一部ではないキーで状態部品をドロップします。それはあなたが経験しているものであり、それはデザインによってどのように動作するのかです。

あなた本当には、あなたのタスクを解決するためにcombineReducersreducerReducersの両方に固執する必要があり、その後、あなたは3つのレデューサーを実装する必要があります場合は、次のスライスAのための1つを、それらの両方で動作しますスライスBと一対一:

const sliceA = (state, action) => state + `| ${action.type} by A`; 
const sliceB = (state, action) => state + `| ${action.type} by B`; 
const full = (state, action) => ({ sliceA: state.sliceA + `| ${action.type} by C`, sliceB: state.sliceB + `| ${action.type} by C` }); 

const reducer = reduceReducers(
    combineReducers({ 
    sliceA, 
    sliceB, 
    }), 
    full 
); 

const initialState = { sliceA: 'initial A', sliceB: 'initial B' }; 
const action = { type: 'touched' }; 

console.log(reducer(initialState, action)); 

// outputs: 

Object { 
    sliceA: "initial A| touched by A| touched by C", 
    sliceB: "initial B| touched by B| touched by C" 
} 

しかし、ちょうどそれを自分で書くことが容易になることがあります:

const sliceA = (state, action) => state + `| ${action.type} by A`; 
const sliceB = (state, action) => ({ 
    sliceA: state.sliceA + `| ${action.type} by B`, 
    sliceB: state.sliceB + `| ${action.type} by B`, 
}) 

const reducer = (state, action) => sliceB(
    { 
    sliceA: sliceA(state.sliceA, action), 
    sliceB: state.sliceB, 
    }, 
    action 
) 
const initialState = { sliceA: 'initial A', sliceB: 'initial B' }; 
const action = { type: 'touched' }; 

console.log(reducer(initialState, action)); 

// outputs: 
Object { 
    sliceA: "initial A| touched by A| touched by B", 
    sliceB: "initial B| touched by B" 
} 
関連する問題