zoukankan      html  css  js  c++  java
  • React V16.x 生命周期调整

    • 旧声明周期:
    生命周期 属于阶段 调用次数 是否可以setState 作用
    getDefaultProps 创建阶段(Mounting) 1次(全局调用1次) 不可以
    getInitialState 创建阶段(Mounting) 1次 不可以
    componetWillMount 创建阶段(Mounting) 1次 可以;不会触发 re-render
    render 创建阶段(Mounting) 和 更新阶段(Updating) >=1次 不可以
    componentDidMount 创建阶段(Mounting) 1次 可以;触发re-render,影响性能
    componetWillReceiveProps 更新阶段(Updating) >=0 可以
    shouldComponentUpdate 更新阶段(Updating) >=0 不可以 该方法通过返回 true 或者 false 来确定是否需要触发新的渲染。返回 false, 则不会触发后续的 UNSAFE_componentWillUpdate()、render() 和 componentDidUpdate()(但是 state 变化还是可能引起子组件重新渲染)。
    componentWillUpdate 更新阶段(Updating) >=0 不可以
    componentDidUpdate 更新阶段(Updating) >=0 可以;触发re-render,影响性能
    componentWillUnmount 卸载阶段(Unmounting) 1次 不可以
    • 新声明周期:
    生命周期 属于阶段 是否可以setState 作用
    constructor() 创建阶段 Mounting 注意:ES6 子类的构造函数必须执行一次 super()。React 如果构造函数中要使用 this.props,必须先执行 super(props)。
    getDerivedStateFromProps 当创建时、接收新的 props 时、setState 时、forceUpdate 时会执行 注意:v16.3 版本 setState、forceUpdate 时不会执行这个方法,v16.4 版本修复了这个问题。
    • 逐渐废弃的生命周期方法(3个Will):
    componentWillMount()
    componentWillReceiveProps()
    componentWillUpdate()
    

    虽然废弃了这三个生命周期方法,但是为了向下兼容,将会做渐进式调整。
    V16.3 并未删除这三个生命周期,同时还为它们新增以 UNSAFE_ 前缀为别名的三个函数 UNSAFE_componentWillMount()、UNSAFE_componentWillReceiveProps()、UNSAFE_componentWillUpdate()。
    在 16.4 版本给出警告将会弃用 componentWillMount()、componentWillReceiveProps()、componentWillUpdate() 三个函数
    然后在 17 版本将会删除 componentWillMount()、componentWillReceiveProps()、componentWillUpdate() 这三个函数,会保留使用 UNSAFE_componentWillMount()、UNSAFE_componentWillReceiveProps()、UNSAFE_componentWillUpdate()

  • 相关阅读:
    python 操作 mysql 数据库 datetime 属性字段为 0000-00-00 00:00:00 的问题
    git 学习笔记
    SB Admin 2 学习笔记1
    docker容器与容器云读书笔记1
    urlencode遇到中文编码问题
    flask_sqlalchemy 乱码问题
    centos6u3 安装 celery 总结
    flask_sqlalchemy 命名遇到的一个小坑
    使用 bash 创建定时任务
    tornado 异步调用系统命令和非阻塞线程池
  • 原文地址:https://www.cnblogs.com/cag2050/p/9692757.html
Copyright © 2011-2022 走看看