我有一个复杂的状态,根据某些条件调用其他减速器函数。在其他减速器中调用减速器是不好的做法吗?
我正在制作一个包装调度调用的自定义钩子,因为我可能需要一些其他逻辑(后端调用、日志记录等)来配合调度。
我在做我现在所做的事情之间左右为难,还是我可以从减速器中取出条件,并在自定义钩子中包含条件。
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)
}
}
在类似的帖子中,有人建议Redux Toolkit Selectors。