我发现 mobx 可观察组件只会重新渲染它们自己,而不是它们的子组件。这是如何实现的?
最佳答案
来自 MobX 文档:
MobX reacts to any an existing observable property that is read during the execution of a tracked function.
通过在组件上使用 @observer
,MobX 将跟踪并响应在该组件的渲染函数中定义的可观察对象发生的变化。
如果您希望每个子组件对更改使用react,都应该用 @observer
包裹。
已编辑:
通过在组件上使用 observer
,MobX 将覆盖 shouldComponentUpdate
,告诉组件仅在必要时更新(可观察值更改或浅层属性更改)。
来自 MobX @observer
documentation :
observer also prevents re-renderings when the props of the component have only shallowly changed, which makes a lot of sense if the data passed into the component is reactive. This behavior is similar to React PureRender mixin, except that state changes are still always processed. If a component provides its own shouldComponentUpdate, that one takes precedence.See for an explanation this github issue
关于javascript - mobx 如何防止重新渲染可观察组件的子组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43163569/