React 组件生存周期

React组件有以下生存周期

1、getDefaultProps

 获取默认的props熟悉,最先执行,主要用于初始化props

2、getInitialState

 获取初始的state状态

3、componentWillMount

 新创建组件后即将被渲染

4、render

 组件渲染

5、componentDidMount

 组件完成渲染

6、componentWillUpdate

 已经存在的组件状态发生变化即将被更新

7、componentWillUnmount

 组件将销毁

8、componentWillReceiveProps

接受到props的设置,render之前,再次期间可以设置setState不引起二次渲染,旧的props可以用this.props获取,新的props为该方法的参数

10、shouldComponentUpdate

接收到新的state或props时,render之前调用,此期间可以验证传入的值确定是否允许试图更新。

以下代码可以跟踪组件的执行过程

var Main = React.createClass({
    getDefaultProps:function(){
        console&&console.log('component getDefaultProps',Date.now());
 },
 getInitialState: function () {
        console&&console.log('component getInitialState',Date.now());
 return {};
 },
 componentWillMount: function () {
        console&&console.log('component will mount',Date.now());
 },
 componentDidMount: function () {
        console&&console.log('component did mount',Date.now());
 },
 componentWillUpdate: function () {
        console&&console.log("componentWillUpdate",Date.now());
 },
 componentDidUpdate: function () {
        console&&console.log('component did update',Date.now());
 },
 componentWillUnmount: function () {
        console&&console.log('component will unmount',Date.now());
 },
 updateChildHandler:function(){
        console.log(this.refs);
 },
 render: function () {
        console&&console.log('component render',Date.now());
 return (<div className="main"><h1>hello world</h1><a href="javascript:void(0)" onClick={this.updateChildHandler}>更新孩子</a><RouteHandler ref="childref"/></div>)
    }
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值