美文网首页
React(五)—— 生命周期

React(五)—— 生命周期

作者: 感觉不错哦 | 来源:发表于2019-02-14 09:38 被阅读50次
编写今天文章之前带给大家的就是,我编写的绝大多数都是基础,因为自己也是重新学习了React也没有项目经验,很多坑以及很多方法是极其不熟练的,希望大家之后能找一些书深入学习
React的生命周期与Vue的生命周期,看似简单,实则我认为是很难很复杂的一部分,这个需要长期的工作经验才能领悟
     class HelloComponent extends React.Component{
         
         constructor(props){
           super(props)
         }

         render(){
             return (<h1>React</h1>)
         }
     }

     ReactDOM.render(<HelloComponent/>,document.getElementById('div'))

初始化代码要很熟练了

一个组件完整的生命周期包含实例化阶段、活动阶段、销毁阶段三个阶段。每个阶段又由相应的方法管理。

过程中涉及三个主要的动作术语:

mounting:表示正在挂接虚拟DOM到真实DOM。
updating:表示正在被重新渲染。
unmounting:表示正在将虚拟DOM移除真实DOM。

每个动作术语提供两个函数:

omponentWillMount()
componentDidMount()
componentWillUpdate(object nextProps, object nextState)
componentDidUpdate(object prevProps, object prevState)
componentWillUnmount()

本文主要理解这五个函数,卸载组件先不写

将生命周期方法添加到类中

以后有机会深入,此时了解先后顺序即可

     class HelloComponent extends React.Component{
         
         constructor(props){
           super(props)
           this.state={name:'React'}
           console.log('1....State')
         }

         componentWillMount(){
            console.log('2...componentWillMount');
         }

         componentDidMount(){
            console.log('3...componentDidMount');
         }
         componentWillUpdate(){
            console.log('4...componentWillUpdate');
         }
         componentDidUpdate(){
            console.log('5...componentDidUpdate');
         }

         handleClick(){
            this.setState({name:'Reacts'})
         }

         render(){
             return (
                         <div>
                             <h1> {this.state.name}</h1>
                             <button onClick={this.handleClick.bind(this)}>CHange</button>
                         </div>
                    )

         }
     }

     ReactDOM.render(<HelloComponent/>,document.getElementById('div'))

再强调两个点,return后面返回的HTML有且只有一个根元素,此处为什么加()
如果不加()return 后面一定要跟元素,加()便于我们代码美观一点
ES6的写法函数默认不绑定this,因此在事件中绑定类实例
因为实际开发中不可能就一个事件,还是建议在constructor中绑定事件的this,便于维护

看看效果

页面初始化的时候,更新钩子未触发,挂载钩子成功触发并且state是第一
测试的时候哪怕你更换方法位置也是一样的,因为函数跟位置是没关系的,但是我们按顺序写更容易理解

此时我们点击事件改变state

React变为Reacts 成功触发了更新钩子函数

有了生命周期我们能做许许多多的事情,Vue最常见的就是数据渲染,比如我们现在假装定义一个ajax请求
         tick(){
             console.log('我是打印的数据')
         }

将此函数挂载到生命周期中

         componentWillMount(){
            this.tick() 
            console.log('2...componentWillMount');
         }

相关文章

  • react生命周期测试记录

    1 react生命周期相关的hook 2 测试结果记录 1, react的生命周期个人认为可以分为五个部分:初始...

  • React概念图

    React概念图 React组件生命周期概念图 参考文档:React入门教程 组件生命周期React:组件生命周期...

  • React基础篇之组件的生命周期

    引出生命周期 react生命周期(旧) react生命周期(新) getSnapshotBeforeUpdate的...

  • React生命周期

    React v16.0前的生命周期 React v16.4+ 的生命周期图 React v16.9后这些生命周期钩...

  • 简述React生命周期

    React 适合0基础教程快速上手 github地址 React组件的生命周期(必须掌握) 创建期(五个阶段...

  • React v16 生命周期

    React 16 生命周期 React 16.3 新增的生命周期方法 逐渐废弃的生命周期方法: 一般将生命周期分成...

  • 学习并实现react (4)

    实现生命周期 生命周期介绍 React 生命周期图 React 子组件在父组件下的生命周期流程 实现 compon...

  • React面试题 整理脑图

    react基础 React生命周期 react-router react进阶 react Hooks redux 其他

  • react/vue常见问题整理

    一、react 1. react生命周期 react 16生命周期相对于15的变化:componentWillMo...

  • React 组件生命周期

    组件生命周期 参考阅读: component-lifecycle react组件生命周期过程说明 react 组件...

网友评论

      本文标题:React(五)—— 生命周期

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