[英]React.js Component context of 'this'
I'm using reactjs and an es6 WeakMap
to map state to a specific component. 我正在使用reactjs和es6
WeakMap
将状态映射到特定组件。 I'm doing this by using the this
value of the component as the key in the WeakMap
of the store associated with the component. 我通过使用组件的
this
值作为与组件关联的商店的WeakMap
中的键来实现此WeakMap
。
When a react component unmounts from the DOM and is then remounted, is the remounted component given a new this
context? 当反应从DOM组件卸除,然后重新安装,是赋予了新的重新安装部件
this
上下文?
I've deduced from the react component life cycle documentation that a new instance of the component is created on remount, but it's not explicitly stated as being the case. 我从react组件的生命周期文档中推断出,该组件的新实例是在重新安装时创建的,但并没有明确指出是这种情况。 I'd appreciate it if someone with more experience with react could clarify this for me.
如果有更多反应经验的人可以为我澄清这一点,我将不胜感激。
Also, if anyone believes that conceptually mapping components to state in the way I'm attempting to do is poor design please feel free to chime in. 另外,如果有人认为在概念上将组件映射为要尝试的状态是糟糕的设计,请随时注意。
When a component is unmounted the instance is thrown away and will get garbage collected. 卸载组件后,该实例将被丢弃,并会收集垃圾。 When the component gets mounted again, it will be a new instance.
再次装入组件时,它将是一个新实例。
I'm not sure what your use case is, but I think that you can be fairly certain that this will always be the case, but it's not impossible to think that React would reuse instances as an optimization in the future. 我不确定您的用例是什么,但我认为您可以肯定会一直如此,但并非不可能,React将来会重用实例作为优化。
Why do you need to keep state in a WeakMap instead of storing it inside a component higher up in the hierarchy? 为什么需要将状态保存在WeakMap中,而不是将其存储在层次结构中较高的组件中?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.