React v16.9 中unsafe的生命周期函数

https://zh-hans.reactjs.org/b...

Unsafe的生命周期

  • componentWillMount → UNSAFE_componentWillMounthtml

    • 没有用过,不描述
  • componentWillReceiveProps → UNSAFE_componentWillReceivePropsreact

    • 缘由:redux

      • 在一次渲染周期内,props屡次变化,形成该函数重复调用。
      • 父组件触发子组件从新渲染,即便props未变化,依然会调用该函数
    • 方案dom

      1. 在render中处理最终须要的渲染相关数据函数

        • componentShouldUpdate 仅处理 props 相关
        • 使用memoize
      2. 使用getDerivedStateFromProps,仅会在渲染前调用,返回值应用于state,不经过setState队列。设计

        • packagesreact-domsrcserverReactPartialRenderer.js
  • componentWillUpdate → UNSAFE_componentWillUpdatecode

    • 缘由:component

      • 设计用于保存组件在渲染前的状态,好比读取dom信息中的滚动值等。
      • 但实际中,可能会调用setState或者触发react-redux中的action
    • 方案server

相关文章
相关标签/搜索