美文网首页
React 生命周期函数

React 生命周期函数

作者: 晴天的晴q | 来源:发表于2019-02-24 12:35 被阅读0次

    生命周期函数是指在某一时刻组件会自动调用执行的函数(每一个组件都可以有)

    v16.3 前的生命周期函数如下:

    Initialization:setup props and state,调用 constructor 函数设置 props 和 state。

    Mounting:componentWillMount -> render -> componentDidMount
    (1)componentWillMount 在组件即将被挂载到页面上的时刻自动被执行
    (2)componentDidMount 在组件被挂载到页面后自动被执行

    Updating:
    props:
    componentWillReceiveProps -> shouldComponentUpdate (为true时继续,为false时则没有以下周期函数) -> componentWillUpdate -> render -> componentDidUpdate。
    states:shouldComponentUpdate (为true时继续,为false时则没有以下周期函数) -> componentWillUpdate -> render -> componentDidUpdate。
    (1)componentWillReceiveProps
            1)一个组件要从父组件接收参数
            2)如果这个组件第一次存在于父组件,不会执行
            3)如果这个组件之前已经存在于父组件中,才会执行
    (2)shouldComponentUpdate 在组件被更新之前,会自动被执行(必须 return 一个布尔值)。
    (3)componentWillUpdate 在组件被更新之前,会被自动执行;但会在 shouldComponentUpdate 之后执行,如果 shouldComponentUpdate 返回 true ,它才执行,如果返回 false ,则不执行。
    (4)componentDidUpdate 在组件更新完成之后,会自动被执行。

    Unmounting:componentWillUnmount 在组件将要被从页面中移除的时候,会被执行。

    v16.4 以后:  将componentWillMount,  componentWillReceiveProps,  componentWillUpdate, 替换为 getDerivedStateFromProps, 并且在componentDidUpdate 前 render 后加了一个 getSnapshotBeforeUpdate 。

    相关文章

      网友评论

          本文标题:React 生命周期函数

          本文链接:https://www.haomeiwen.com/subject/ebivyqtx.html