在调度中检测到状态突变:即使使用工具包 [英] A state mutation was detected inside a dispatch: Even with toolkit
问题描述
此操作引发以下错误:
setTimerState(state, { payload }: PayloadAction<{ timer: StepTimer, timerState: CookingTimerState }>) {const { timer, timerState } = 有效载荷const timerInStore = state.stepTimers.find(t => t.timerId === timer.timerId)!timerInStore.state = timerState}
<块引用>
错误:不变失败:在调度中检测到状态突变,路径为:cookingSession.stepTimers.0.state.看看处理动作的减速器 {"type":"session/setTimerState","payload":{"timer":{"label":"Dance","durationSec":600,"stepIndex":0,"timerId":0,"state":2},"timerState":2}}
我认为 Redux 工具包允许您进行这种状态更改.是因为我的 StepTimer
是类对象而不是 POJO 吗?
正确.只有原始值、对象和数组被认为是可序列化的,而类实例不是.
根据 Redux 风格指南,您不应该将类实例置于您的状态中.
This action is throwing the error below:
setTimerState(state, { payload }: PayloadAction<{ timer: StepTimer, timerState: CookingTimerState }>) {
const { timer, timerState } = payload
const timerInStore = state.stepTimers
.find(t => t.timerId === timer.timerId)!
timerInStore.state = timerState
}
Error: Invariant failed: A state mutation was detected inside a dispatch, in the path: cookingSession.stepTimers.0.state. Take a look at the reducer(s) handling the action {"type":"session/setTimerState","payload":{"timer":{"label":"Dance","durationSec":600,"stepIndex":0,"timerId":0,"state":2},"timerState":2}}
I thought Redux toolkit allowed you to make this kind of state change.
Is it because my StepTimer
s are class objects not POJOs?
Correct. Only primitive values, objects, and arrays are considered serializable, and class instances are not.
Per the Redux Style Guide, you shouldn't be putting class instances in your state.
这篇关于在调度中检测到状态突变:即使使用工具包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!