javascript - mobx 如何防止重新渲染可观察组件的子组件?

标签 javascript reactjs mobx

我发现 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/

相关文章:

javascript - state.map(state, i) 给出未定义

reactjs - 使用 mobx 继承 React 组件

javascript - Mobx 和 React。可观察到的变化后如何处理焦点输入?

javascript - 谷歌地图信息窗口移动整个 map

javascript - Dropzone.js - 显示存储在 app_data 文件夹中的图像

javascript - 有没有办法在不同选项卡之间隔离 'localStorage'?

reactjs - 为什么 mobx-react 观察者会破坏 react-router-dom navlink 事件类?

javascript - jquery 之类的代码在 switch 内不起作用

javascript - React 计算器 - 等号逻辑

javascript - 从组件中的 useState 多次调用状态更新程序会导致多次重新渲染