gpt4 book ai didi

redux - 是否可以在切片 reducer 中重新分配整个状态(使用 Redux ToolKit)?

转载 作者:行者123 更新时间:2023-12-03 08:26:25 25 4
gpt4 key购买 nike

我正在使用 Redux Toolkit 的 createSlice()。并尝试创建一个 reducer ,将外部源的数据填充到 redux 状态。我试图通过在操作负载中传入一个对象来更新整个状态:

.
reducers: {
populateData: (state, action) => {
state = action.payload
}
.

但只有当我为每个对象键创建一个 reducer 时它才会起作用。有没有办法一次性完成这一切?

最佳答案

这是一个常见的误解,实际上我刚刚写了一个新的"Writing Reducers with Immer" docs page昨天回答了这个问题。引用一下:

A common mistake is to try assigning state = someValue directly. This will not work! This only points the local state variable to a different reference. That is neither mutating the existing state object/array in memory, nor returning an entirely new value, so Immer does not make any actual changes.

相反,您需要:返回action.payload,这将完全替换现有状态。

关于redux - 是否可以在切片 reducer 中重新分配整个状态(使用 Redux ToolKit)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66532826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com