我有一个复杂的状态,其中基于某些条件调用其他归约函数. 在其他还原器中调用还原器是否是不好的做法?
我正在制作一个自定义挂钩来包装调度调用,因为我可能希望一些其他逻辑(后台调用、日志(log)记录等)与调度一起运行.
我在做我现在所做的事情之间犹豫不决,或者我可以将条件句从简化器中取出,然后在自定义挂钩中添加条件句.
根据我的研究,我可以做一些事情.
- 让它保持原样
- 将逻辑提炼成thunks,简化简化器
- 可能使用 Select 器来计算派生数据,但这可能无法按照我想要的方式更新我的状态
export const game = createSlice({
name: 'game',
initialState,
reducers: {
incrementScale: (state) => {
state.currentScale = state.scales.pop()
if (!state.currentScale) {
state.isGameInProgress = false
} else {
game.caseReducers.incrementNote(state)
}
},
incrementNote: (state) => {
state.currentNote = state.notes.shift()
if (!state.currentNote) {
game.caseReducers.incrementScale(state)
}
state.triesLeft = INTIAL_TRIES
}
}
})
定制挂钩
export function useGame() {
const handleIncrementNote() {
//put the undefined checks and other logic in here instead?
dispatch(incrementNote)
}
}