简体   繁体   English

破坏 redux 减速器中的默认 state

[英]Destructing default state in redux reducer

What is the difference in returning state in default case in redux reducer between return state and return {...state } ?在 redux 减速器的默认情况下,返回 state 在return statereturn {...state }之间有什么区别?

Like React, Redux uses immutability to efficiently check updated object references for updated state, which is why you should never modify/mutate state directly. Like React, Redux uses immutability to efficiently check updated object references for updated state, which is why you should never modify/mutate state directly. In the default case of most reducers no actions have modified the state so you can return the original state object ( return state ), this is safe as it won't have been modified. In the default case of most reducers no actions have modified the state so you can return the original state object ( return state ), this is safe as it won't have been modified.

However, return {...state } will return an identical state but with a different top level object reference which will cause unnecessary checks for changed state.但是, return {...state }将返回相同的 state 但具有不同的顶级 object 引用,这将导致对已更改的 state 引用进行不必要的检查If you're not modifying state at all you should always return the original object ( return state ).如果您根本不修改 state ,则应始终返回原始 object ( return state )。

EDIT: added on a follow up question - if you return {...state } Redux would broadcast the updated state to all React components connected to the store (be it by hooks or the connect function) and then React would go into it's lifestyle cycle update. EDIT: added on a follow up question - if you return {...state } Redux would broadcast the updated state to all React components connected to the store (be it by hooks or the connect function) and then React would go into it's lifestyle周期更新。 React is actually very efficient and uses memoizing and other methods to stop any expensive re-renders or repainting/regenerating the DOM so the difference between the 2 in terms of process will be next to nothing, it's just unnecessary. React 实际上非常高效,它使用记忆和其他方法来阻止任何昂贵的重新渲染或重新绘制/重新生成 DOM,因此两者在流程方面的差异几乎没有,这只是不必要的。 I don't think it'd even get to rerunning hooks suck as useEffect, it'd see the redundancy before getting there or at least use the memoize cache and not recalculate我认为它甚至不会像 useEffect 那样重新运行钩子,它会在到达那里之前看到冗余,或者至少使用 memoize 缓存而不重新计算

They are both the same when setting the default state.设置默认 state 时,它们都是相同的。 return {...state } will make sense when you actually change the state like return {...state }当您实际更改 state 时将有意义

return {
    ...state, 
    valueA: "a"
}

This will update the whole state with the new object which has a updated property valueA and keping all others as existing values.这将使用新的 object 更新整个 state,它具有更新的属性valueA并将所有其他属性保留为现有值。

ES6 has added spread property to object literals in javascript. ES6在 javascript 中为 object 文字添加了扩展属性。 The spread operator (…) with objects is used to create copies of existing objects with new or updated values or to make a copy of an object with more properties.带有对象的扩展运算符(…)用于创建具有新值或更新值的现有对象的副本,或者用于制作具有更多属性的 object 的副本。

Let's take at an example of how to use the spread operator on an object,让我们以如何在 object 上使用扩展运算符为例,

const user1 = {
    name: 'Jen',
    age: 22
};

const clonedUser = { ...user1 };
console.log(clonedUser);

输出

Here we are spreading the user1 object.这里我们传播user1 object。 All key-value pairs of the user1 object are copied into the clonedUser object. user1 object 的所有键值对都复制到clonedUser object 中。 Let's look on another example of merging two objects using the spread operator,让我们看另一个使用扩展运算符合并两个对象的示例,

const user1 = {
    name: 'Jen',
    age: 22,
};

const user2 = {
    name: "Andrew",
    location: "Philadelphia"
};

const mergedUsers = {...user1, ...user2};
console.log(mergedUsers)

在此处输入图像描述

mergedUsers is a copy of user1 and user2 . mergedUsersuser1user2的副本。 Actually, every enumerable property on the objects will be copied to mergedUsers object.实际上,对象上的每个可枚举属性都将被复制到mergedUsers object。 The spread operator is just a shorthand for the Object.assign() method but, they are some differences between the two.扩展运算符只是Object.assign()方法的简写,但它们是两者之间的一些区别。

To know more, visit Redux |要了解更多信息,请访问Redux | Using Object Spread Operator使用 Object 扩展运算符

When you use return state you are returning the variable, this is bad because if that variable changes, the value delivered in the return will also change (this is known as a side effect).当您使用return state时,您正在返回变量,这很糟糕,因为如果该变量发生变化,返回中传递的值也会发生变化(这被称为副作用)。

On the other hand, if you use return {... state } what you are returning is a copy of the value of the variable at that moment另一方面,如果您使用return {... state }您返回的是当时变量值的副本

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

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