ReactJS アプリがTypeScript
あり、レデューサーを構築するためにredux
使用しています。redux-toolkit
アプリが大きくなったので、レデューサーのリファクタリングを開始したいと考えています。
私の還元状態は次のようになります。
{
customers: Customers[],
orders: {
state1: SomeIndependentState1,
state2: SomeIndependentState2,
state3: SomeDependentState2,
state4: SomeDependentState3,
}
}
customers
とのorders
スライスは独立しているので、2 つの個別のレデューサーを簡単に作成して、combineReducers
後で組み合わせることができます。
orders
ここで、減速機をさらに分解したいと思います。
state1
そしてstate2
完全に独立しています。state3
からのデータに依存しますstate1
。state4
state1
とからのデータに依存しますstate2
。
createReducer
from redux-toolkit
(またはツールキットの他の機能)を引き続き使用して、スライス内のネストされorders
た各スライスのレデューサーを作成する方法はありますか?
のレデューサーを書き直し始めたorders
ので、これまでのところ次のとおりです。
export const ordersReducer = (state: Orders, action: AnyAction) => {
return {
state1: state1Reducer(state?.state1, action),
state2: state2Reducer(state?.state2, action),
state3: {}, // not sure how to write a reducer for this slice and satisfy its dependency on state1
state4: {}, // not sure how to write a reducer for this slice and staisfy its dependency on state1 and state2
}
};
const initialState1: State1 = {};
export const state1Reducer = createReducer(initialState1, (builder) =>
builder
.addCase(...)
.addCase(...)
);
const initialState2: State2 = {};
export const state2Reducer = createReducer(initialState2, (builder) =>
builder
.addCase(...)
.addCase(...)
);
注: 還元状態の構造を制御することはできません。私は使用に完全に縛られているわけではありませんredux-toolkit
が、チームがそれを使用しないようにするためには正当な理由が必要です。