- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是ngrx的新手(并且从未使用过redux),并且正在尝试理解所有内容-尤其是您是否需要状态的深拷贝。这是到目前为止我学到的东西,仍然让我感到困惑(进一步以粗体显示)。
Ngrx文档state
Each reducer function takes the latest Action dispatched, the current state, and determines whether to return a newly modified state or the original state.
Each action handles the state transition immutably. This means that the state transitions are not modifying the original state, but are returning a new state object using the spread operator.
The spread syntax copies the properties from the current state into the object, creating a new reference. This ensures that a new state is produced with each change, preserving the purity of the change. This also promotes referential integrity, guaranteeing that the old reference was discarded when a state change occurred.
OnPush
。
If the returned object reference has changed, it will trigger any related RxJS state subscriptions for the particular piece of state in question. Which subscriptions are triggered can be minimised using some good ngrx selectors.
enables sophisticated change detection techniques to be implemented simply and cheaply, ensuring the computationally expensive process of updating the DOM occurs only when it absolutely has to
Note: The spread operator only does shallow copying and does not handle deeply nested objects. You need to copy each level in the object to ensure immutability. There are libraries that handle deep copying including lodash and immer.
This question even extends to the way Angular's
ngFor
change detection works (and using atrackBy
function complicates that even further!): when I clone every item inThing[]
and have my reducer return a new list of clonedThing
s, Angular will think it's a brand new list (which it technically is) and run change-detection for all items in the list: They will also be brand new, and as such, old list items get removed and new ones get added to the DOM.Suppose you have a
ThingComponent
for eachThing
in thengFor
list. In that component,ngOnChanges
will fire. But here's the thing: theSimpleChanges
passed tongOnChanges
will never containpreviousValues
, because the whole list got replaced, and so there is previous value: everything is brand new, from Angulars perspective.
trackBy
),但是我现在想知道:
最佳答案
关于angular - 进行深度复制还是不进行深度复制-无论如何,为什么ngrx的状态应该是不可变的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59577923/
COW 不是奶牛,是 Copy-On-Write 的缩写,这是一种是复制但也不完全是复制的技术。 一般来说复制就是创建出完全相同的两份,两份是独立的: 但是,有的时候复制这件事没多大必要
我是一名优秀的程序员,十分优秀!