常用的React生命周期函数

生命周期的三个阶段

  1. Mounting 加载阶段
  2. Update 更新阶段
  3. UnMounting 卸载阶段

常用的React生命周期函数

  1. componentWillMount 第一次渲染之前
  2. componentDidMount 第一次渲染之后
  3. componentWillReceiveProps(nextProps){} 在组件接受新的props调用
componentWillReceiveProps(nextProps){
	if(this.props.num  !== nextProps.num){
		//处理逻辑
	}
}
  1. shouldComponentUpdate(nextProps,nextState){} 返回一个布尔值,在组件接受一个新的props或者新的state调用。
shouldComponentUpDate(nextProps,nextState){
	if(nextProps.num !== this.props.num){
		return true;
	}
	return false;
}
  1. componentWillUpdate(nextProps,nextState){} 在组件接受新的state和新的props,但还没render的时候调用。
componentWillUpdate(nextProps,nextState){
	
}
  1. componentDidUpdate(prevProps,prevState){} 在组件更新之后调用。
componentDidUpdate(prevProps,prevState){
	
}
  1. componentWillUnMount() 在组件卸载之前立即调用。

示例:

/**
 * Created by mapbar_front on 2019/8/26.
 */
import React,{Component} from 'react';
import {Button} from 'antd';



class ShowNum extends Component{
    constructor(props){
        super(props);
    }
    componentWillReceiveProps(nextProps){
        console.log('---receive props---');
        console.log('this.props',this.props);
        console.log('nextProps',nextProps);
    }
    shouldComponentUpdate(newProps){
        console.log('新的newProps');
        if(newProps.num !== this.props.num){
            console.log('newProps.num',newProps.num);
            console.log('this.props.num',this.props.num);
            return true;
        }else {
            console.log('else');
            console.log('newProps.num',newProps.num);
            console.log('this.props.num',this.props.num);
        }
        return false;
    }
    componentDidUpdate(prevProps,prevState){
       console.log('---componentDidUpdate2---');
       console.log('prevProps',prevProps);
       console.log('prevState',prevState);
    }
    render(){
        return(
            <div>
                {this.props.num}
            </div>
        )
    }
}
class LifeCycle extends Component{
    constructor(props){
        super(props);
        this.state = {
            num:0,
        }
    }
    componentWillMount(){
        console.log('will mount');
    }
    componentDidMount(){
        console.log('did mount');
    }
    shouldComponentUpdate(newProps,newState){
        console.log('---should update---');
        console.log('newProps',newProps);
        console.log('newState',newState);
        return true;
    }
    componentWillUpdate(nextProps,nextState){
        console.log('---will update---');
    }
    componentDidUpdate(prevProps,prevState){
        console.log('---did update---');
        console.log('prevProps',prevProps);
        console.log('prevState',prevState);
    }
    addNum=()=>{
        this.setState({
            num: this.state.num+1
        })
    }
    render(){
        return(
            <div>
                <Button onClick={this.addNum}>
                    LifeCycle
                </Button>
                <ShowNum num={this.state.num}/>
            </div>
        )
    }
}
export default LifeCycle;

react17版本新增的两个生命周期函数是:

  1. List item

参考文章

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值