react生命周期

三大生命周期状态:

                        Mount:插入真实Dom

                         Update:被重新渲染

                         Unmount:被移除真实Dom

class Life extends React.component{

    constructor(props){

       super(props);

     console.log('constructor()--->创建组件对象');

   this.state={

        name:'Tom' 

    }

   }  

 componentWillMount(){

  console.log('componentWillMount()--->将要挂载') ;

  //延时更新state

  // setTimeout(function(){ this.setState({name:'Linda'})}.bind(this),3000)

  setTimeout(()=>{this.setState({name:'Linda'})},3000)

//延时移除组件

 setTimeout(()=>{

     ReactDom.unmountComponentAtNode(document.getElementById('root')) 

  },6000)

}

   render(){

      console.log('render()-->正在渲染中')

      return (

            <h2>生命周期测试组件</h2>

           <h2>姓名:{this.state.name}</h2>

            <h2>年龄:{this.props.age.}</h2>

         )

    }

 componentDidMount(){

    console.log('componentDidMount()-->已经挂载,这时发送ajax请求')

  }

componentWillUpdate(){

   console.log('componentWillUpdate()-->将要更新')  

}

componentDidUpdate(){

   console.log('componentDidUpdate()-->已经更新')  

}

componentWillUnmount(){

   console.log('componentWillUnmount()-->将要被移除')

  }

componentWillReceWillReceiveProps(){

   }

}

ReactDom.render(<Life age={12}>,document.getElementById('root'))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值