react-native生命周期详解

3,771 阅读5分钟

这是我参与更文挑战的第13天,活动详情查看:更文挑战

生命周期的3个状态:

Mounting: 将组件插入到DOM中

Updating: 将数据更新到DOM中

Unmounting: 将组件移除DOM中

生命周期中的钩子函数(方法、事件)

CompontWillMount: 组件将要渲染

CompontDidMount: 组件渲染完毕

CompontWillReceiveProps: 组件将要接收props数据

ShouldComponentUpdate: 组件接收到新的state或者props,判断是否更新,返回布尔值

CompontWillUpdate: 组件将要更新

ComponentDidUpdate: 组件已经更新

ComponentwillUnmount: 组件将要卸载

主要的生命周期

• constructor

  • 组件被实例化的时候触发, 一般用做对组件做初始工作,如设置state等

• render

  • 组件开始渲染时触发
  • 组件被更新时触发 (state和props发生改变时触发)

• componentDidMount

  • 组件挂载完毕,可以发送异步请求获取数据

• componentWillUnmount

  • 组件被卸载时触发
  • 一般用在清除定时器或者取消订阅等
import React from 'react'
 import ReactDom from 'react-dom'

 class ComLife extends React.Component {
     constructor(props) {
         super(props)  // 调用继承的Component的构造函数
         this.state = {
             msg: 'hello world'
         }
         console.log('constructor 构造函数')  // 1
     }

     componentWillMount() {
         console.log('componentWillMount  组件将要渲染')  // 2
     }

     componentDidMount() {
         console.log('componentDidMount 组件渲染完毕')  // 4
     }

     componentWillReceiveProps() {
         console.log('componentWillReceiveProps 组件将要接收新的state和props')
     }

     componentWillUpdate() {
        console.log('componentWillUpdate 组件将要更新')
     }

     componentDidUpdate() {
        console.log('componentDidUpdate 组件更新完毕')
     }

     componentWillUnmount() {
         console.log('componentWillUnmount 组件将要移除')
     }

     render() {
         console.log('render渲染函数')  // 3
         return (
             <div>
                 <h1>hello world</h1>
             </div>
         )
     }
 }

 //  输出
// constructor 构造函数
// componentWillMount  组件将要渲染
// render渲染函数
// componentDidMount 组件渲染完毕

 ReactDom.render(<ComLife />, document.getElementById('root'))

当组件等进行值的更新的时候

//更新控制

 import React from 'react'
 import ReactDom from 'react-dom'

 class ComLife extends React.Component {
     constructor(props) {
         super(props)  // 调用继承的Component的构造函数
         this.state = {
             msg: 'hello world'
         }
         console.log('constructor 构造函数')  // 1
     }

     componentWillMount() {
         console.log('componentWillMount  组件将要渲染')  // 2
     }

     componentDidMount() {
         console.log('componentDidMount 组件渲染完毕')  // 4
     }

     componentWillReceiveProps() {
         console.log('componentWillReceiveProps 组件将要接收新的state和props')
     }

     // 更新控制器
     shouldComponentUpdate() {
         // 如果希望更新,就返回为真, 不希望更新就返回为false
         if(this.state.msg == '测试') {  //eslint-disable-line
             return true
         } else {
             return false
         }
     }

     componentWillUpdate() {
        console.log('componentWillUpdate 组件将要更新')
     }

     componentDidUpdate() {
        console.log('componentDidUpdate 组件更新完毕')
     }

     componentWillUnmount() {
         console.log('componentWillUnmount 组件将要移除')
     }

     render() {
         console.log('render渲染函数')  // 3
         return (
             <div>
                 <h1>{ this.state.msg }</h1>
                 <span onClick={ this.onChangeComponentMount }>更新数据</span>
             </div>
         )
     }
     onChangeComponentMount = () => {
         this.setState({ msg: '测试' })
     }
 }

 //  输出
// constructor 构造函数
// componentWillMount  组件将要渲染
// render渲染函数
// componentDidMount 组件渲染完毕

 ReactDom.render(<ComLife />, document.getElementById('root'))

1. 挂载卸载过程

1.1.constructor()

constructor()中完成了React数据的初始化,它接受两个参数:props和context,当想在函数内部使用这两个参数时,需使用super()传入这两个参数。

注意:只要使用了constructor()就必须写super(),否则会导致this指向错误。

1.2.componentWillMount()

componentWillMount()一般用的比较少,它更多的是在服务端渲染时使用。它代表的过程是组件已经经历了constructor()初始化数据后,但是还未渲染DOM时。

1.3.componentDidMount()

组件第一次渲染完成,此时dom节点已经生成,可以在这里调用ajax请求,返回数据setState后组件会重新渲染

1.4.componentWillUnmount ()

在此处完成组件的卸载和数据的销毁。

  1. clear你在组建中所有的setTimeout,setInterval
  2. 移除所有组建中的监听 removeEventListener
  3. 有时候我们会碰到这个warning:

Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the undefined component.

原因:因为你在组件中的ajax请求返回setState,而你组件销毁的时候,请求还未完成,因此会报warning

解决方法:
componentDidMount() {
    this.isMount === true
    axios.post().then((res) => {
    this.isMount && this.setState({   // 增加条件ismount为true时
          aaa:res
        })
    })
}
componentWillUnmount() {
    this.isMount === false
}

2. 更新过程

2.1. componentWillReceiveProps (nextProps)

  1. 在接受父组件改变后的props需要重新渲染组件时用到的比较多
  2. 接受一个参数nextProps
  3. 通过对比nextProps和this.props,将nextProps的state为当前组件的state,从而重新渲染组件
componentWillReceiveProps (nextProps) {
    nextProps.openNotice !== this.props.openNotice&&this.setState({
        openNotice:nextProps.openNotice
    },() => {
      console.log(this.state.openNotice:nextProps)
      //将state更新为nextProps,在setState的第二个参数(回调)可以打         印出新的state
  })
}

2.2.shouldComponentUpdate(nextProps,nextState)

1. 主要用于性能优化(部分更新)

2. 唯一用于控制组件重新渲染的生命周期,由于在react中,setState以后,state发生变化,组件会进入重新渲染的流程,在这里return false可以阻止组件的更新

3. 因为react父组件的重新渲染会导致其所有子组件的重新渲染,这个时候其实我们是不需要所有子组件都跟着重新渲染的,因此需要在子组件的该生命周期中做判断

2.3.componentWillUpdate (nextProps,nextState)

shouldComponentUpdate返回true以后,组件进入重新渲染的流程,进入componentWillUpdate,这里同样可以拿到nextProps和nextState。

2.4.componentDidUpdate(prevProps,prevState)

组件更新完毕后,react只会在第一次初始化成功会进入componentDidmount,之后每次重新渲染后都会进入这个生命周期,这里可以拿到prevProps和prevState,即更新前的props和state。

2.5.render()

render函数会插入jsx生成的dom结构,react会生成一份虚拟dom树,在每一次组件更新时,在此react会通过其diff算法比较更新前后的新旧DOM树,比较以后,找到最小的有差异的DOM节点,并重新渲染。

人懒,不想配图,都是自己的博客内容(干货),望能帮到大家

公众号:小何成长,佛系更文,都是自己曾经踩过的坑或者是学到的东西

有兴趣的小伙伴欢迎关注我哦,我是:何小玍。 大家一起进步鸭