[英]How React keeps the component state?
This is kind of a question about the internals of react.js.这是一个关于 react.js 内部的问题。 Here is how I understand the virtual dom.
以下是我对虚拟 dom 的理解。
old (rendered from component <X/>) new (rendered from component <X/>)
null <div>
<AnotherComponent/> <- state {count:0}
<p>{this.state.text}</p>
</div>
during comparison the function sees that the old snapshot is empty.在比较过程中,该函数发现旧快照是空的。 we create our elements, and elements from the component
<AnotherComponent/>
.我们创建我们的元素,以及来自组件
<AnotherComponent/>
元素。
old (rendered from component <X/>) new (rendered from component <X/>)
<div> <div>
<AnotherComponent/> <- state {count:1} <AnotherComponent/> <- state {count:0} ?
<p>Lorem ipsum</p> <p>Lorem ...</p> <- state updates
</div> </div>
let's say the <X/>
component is updated for the second time, and <AnotherComponent/>
has changed its state before the update of the parent.假设
<X/>
组件第二次更新,并且<AnotherComponent/>
在父组件更新之前已经改变了它的状态。
okay, i compare the two snapshots, apply the changes.好的,我比较两个快照,应用更改。 but
<AnotherComponent/>
will reset its state and I will have {count:0}
instead of {count:1}
.但是
<AnotherComponent/>
将重置其状态,我将使用{count:0}
而不是{count:1}
。
how react.js or any other declarative js library for building ui do kind of stuff. react.js 或任何其他用于构建 ui 的声明性 js 库是如何做一些事情的。 maybe I'm getting something wrong.
也许我有什么问题。
i think I need to get the state of the old component and assign it to a new component.我想我需要获取旧组件的状态并将其分配给新组件。 is this the right way to do it?
这是正确的方法吗? thanks!
谢谢!
据我所知,React 使用闭包来捕获重新渲染之间的状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.