简体   繁体   English

为什么 slice push 在 redux 中有效,但在 concat 中无效?

[英]Why does slice push work in redux but not concat?

Recently I change to slice and I seeing some odd behavior can someone explain:最近我换成了切片,我看到一些奇怪的行为有人可以解释一下:

const initialState = {
  []
};

export const userSlice = createSlice({
  name: "user",
  initialState,
  reducers: {
    loadUsers(state, action) {
      state = state.concat(action.payload);
    },
  },
});

when i do something like this the state wont change and payload dont add to state array当我做这样的事情时,state 不会改变,有效负载也不会添加到 state 数组

i can go like我可以 go 喜欢

for(let i=0; i < action.payload.length; i++) {
    state.push(action.payload[i]
}

and everything work fine, then I realized if name the state initial state like:一切正常,然后我意识到如果将 state 初始 state 命名为:

const initialState = {
  users: [],
};

Then I can go like:然后我可以像 go 这样:

state.users = state.users.concat(action.payload);

on loadUsers reducer and this work fine too can someone explain why concat wont work on first attempt when i have initial state of [] without naming itloadUsers reducer 上,这项工作也很好有人可以解释为什么concat在我有 [] 的初始 state 而没有命名时第一次尝试时无法工作

The problem here is not push or concat , but the fact that这里的问题不是pushconcat ,而是事实

state = something

will never do anything.永远不会做任何事情。

Redux Toolkit watches the object in the variable state for modifications - but that assignment throws away that object and puts a new object into the variable instead of changing it. Redux Toolkit 监视处于变量state的对象以进行修改 - 但该赋值会丢弃该对象并将新对象放入变量中而不是更改它。

That cannot be observed.这是无法观察到的。

Instead, you can do相反,你可以做

return something

For more information, see Writing Reducers with immer , especially resetting and replacing state , which says:有关更多信息,请参阅使用immer编写immer ,尤其是重置和替换 state ,其中说:

A common mistake is to try assigning state = someValue directly.一个常见的错误是尝试直接分配state = someValue 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.这既不会改变内存中现有的状态对象/数组,也不会返回全新的值,因此 Immer 不会进行任何实际更改。

Adding on to the previous answer, to make one distinction.添加到先前的答案,以进行区分。 You should not attempt to mutate the entire state as in the line你不应该像在行中那样尝试改变整个 state

State = something

But you can and should mutate state properties in this way, in fact it is one of the major benefits of redux toolkit and immer, so但是你可以而且应该以这种方式改变 state 属性,事实上它是 redux 工具包和 immer 的主要好处之一,所以

State.prop = something 

Is ok.没问题。 This is a good time to reiterate this kind of state mutation is only ok in redux toolkit where immer is doing it's work.现在是重申这种 state 突变的好时机,只有在 redux 工具包中 immer 正在做它的工作。

Again here is the link to the relevant docs同样,这里是相关文档的链接

https://redux-toolkit.js.org/usage/immer-reducers#resetting-and-replacing-state https://redux-toolkit.js.org/usage/immer-reducers#resetting-and-replacing-state

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM