最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • react-native生命周期详解

    正文概述 掘金(何小玍。)   2021-06-23   571

    这是我参与更文挑战的第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:

    原因:因为你在组件中的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节点,并重新渲染。

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

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

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


    起源地下载网 » react-native生命周期详解

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元