react生命周期的学习与总结

框架的生命周期就是特定阶段调用的钩子函数 有了生命周期才像一个框架
一个组件的生命周期有四个
1.initial 执行初始化 props state都有了
2.mounting 挂载阶段 【要挂载吗】有一个render
3.updating 修改 【要修改吗】 shouldComponentUpdate(nextProps,nextState)
4.unmounted 卸载

组件首次加载时执行顺序(react16.3之后一下加粗生命周期被取消)
componentWillMount -> render - > componentDidMount
组件更新时
shouldComponentUpdate - > componentWillUpdate - > render -> componentDidUpdate
如果props更新了会在shouldComponentUpdate前加一个componentWillRceiveProps

react16.3之后
首次加载
static getDrivedStateFromProps - > render -> componentDidMount
组件跟新
static getDrivedStateFromProps - > shouldComponnentUpdate - >render -> getSnapShotBeforeUpdate - > componentDidUpdate

// 组件即将被渲染 装载到真正的页面上
    componentWillMount() {}
// 组件被真正装载
    componentDidMount() {}

// 组件要更改吗
    shouldComponentUpdate(nextProps, nextState) {
        // 在前后name值不相等的时候才继续重新渲染
        if (nextState.count != this.state.count) {
            console.log("shouldComponentUpdate")
            return true
        } else {
            return false
        }
    }
//组件即将更新
    componentWillUpdate() {}
// 组件已经更新
    componentDidUpdate() {}

//组件即将被销毁
    componentWillUnmount() {}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值