【问题标题】:Does this.forceUpdate rerender all children?this.forceUpdate 是否重新渲染所有子级?
【发布时间】:2017-10-27 12:42:40
【问题描述】:

有关 forceUpdate 的文档有以下说法:

调用 forceUpdate() 将导致 render() 在 组件,跳过 shouldComponentUpdate()。这将触发 子组件的正常生命周期方法,包括 每个孩子的 shouldComponentUpdate() 方法。 React 仍然只会 如果标记发生变化,则更新 DOM。

https://reactjs.org/docs/react-component.html#forceupdate

这是否意味着组件的子组件也调用了它们的渲染函数?

【问题讨论】:

  • 是的,除非他们的shouldComponentUpdate 决定不需要重新渲染。 forceUpdate 只会绕过调用forceUpdate 的组件上的shouldComponentUpdate。

标签: javascript reactjs react-native


【解决方案1】:

它将“重新渲染”每个在其 shouldComponentUpdate 方法中不返回 false 的子节点,但请注意 re-rendering !== re-drawing,它只会导致 React 的 diffing 算法运行。

只有当差异算法发现当前版本的虚拟 DOM 与之前版本的虚拟 DOM 存在差异时,才会在屏幕上重新绘制 UI 的特定“更改”部分。

【讨论】:

    猜你喜欢
    • 2017-03-06
    • 2021-04-14
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 2021-02-26
    • 2018-12-28
    • 2021-05-08
    • 2012-07-16
    相关资源
    最近更新 更多