react生命周期解读

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./base/react.min.js"></script>
    <script src="./base/react-dom.min.js"></script>
    <script src="./base/browser.min.js"></script>
</head>
<body>
   <div id="app"></div>
   <script type='text/babel'>
		let Father = React.createClass({
			/*
			    getDefaultProps 
			    不能获取元素  
			    不能获取组件对象  
			    优先级低(传参的优先级高) 
			    只执行一次 不管组件实例化多少次props默认都是共享
			*/ 
			getDefaultProps(){
				return{
					value:0,
				}
			},
			/*
			    getInitialState
			    可以获取this.props
			    不能获取元素 
			    不能获取state的值 通过console.log 可以看到
			*/ 
			getInitialState(){
				console.log(this)
				return {
					value:this.props.value+1
				}
			},
			/*
			    componentWillMount
			    只会在装载之前调用一次
			    最后一次修改数据机会 在页面挂载之前
			    在这里修改状态值  触发的不是 运行中的生命周期
			    可以获取this 
			    不能获取元素
			    获取数据
			*/ 
			componentWillMount(){
				console.log('componentWillMountStart',this.props.value,this.state.value);
				this.props.value++;
				this.state.value++;
				console.log('componentWillMountEnd',this.props.value,this.state.value);
			},
			/*
			    componentDidMount
			    只会在装载之后调用一次
			    可以获取this 
			    可以获取元素
			    可以获取数据
			    在render之后调用
			*/ 
			componentDidMount(){
				console.log('componentDidMountStart',this.props.value,this.state.value);
				this.props.value++;
				this.state.value++;
				console.log('componentDidMountEnd',this.props.value,this.state.value);
				this.setState({});
			},
			/*
				见Son组件
			*/
			componentWillReceiveProps(){
				console.log("componentWillReceiveProps被调用了",this.props.value)
			},
			/*
				见Son组件
			*/
			shouldComponentUpdate(props,state){
				console.log("shouldComponentUpdate被调用了",this.props.value,props.value,this.state.value,state.value,"true----false")
				return true;
			},
			/*
				页面即将发生更新之前调用
			*/
			componentWillUpdate(){
				console.log("componentWillUpdate被调用了",this.props.value)
			},
			/*
				页面发生更新之后调用
			*/
			componentDidUpdate(){
				console.log("componentDidUpdate被调用了",this.props.value)
			},
			/*
				见Son组件
			*/
			componentWillUnmount(){
				console.log("即将销毁")
			},
			changeProps(){
				console.log("changeProps被调用了",this)
				this.props.value++
				this.setState({});
			},
			changeState(){
				console.log("changeProps被调用了",this)
				
				this.setState({
					value:++this.state.value
				});
			},
			destruction(){
				console.log("destruction被调用了",this,this.state.show)
				
				this.setState({
				show:!this.state.show
				});
			},
			render(){
				return(
					<div>
						this is props : {this.props.value}
						<br/>
						this is state : {this.state.value}
						<br/>
						<button onClick={this.changeProps}>增加props值</button>
						<button onClick={this.changeState}>增加state值</button>
						<hr/>
						<button onClick={this.destruction}>销毁Son</button>
						{this.state.show||<Son value={this.props.value} change={this.changeProps}></Son>}
					</div>
				)
			}
		})

		let Son = React.createClass({
			/*
			    componentWillReceiveProps
			   	当父级传递的值发生改变时才会触发,自生修改props不会触发
			*/ 
			componentWillReceiveProps(){
				console.log("Son-----componentWillReceiveProps被调用了",this.props.value)
			},
			/*
			    shouldComponentUpdate
			   	当修改父级的props时才会记录上一次值与即将改为的值,修改自身接收到props数据无法记录上一次值
			*/ 
			shouldComponentUpdate(props,state){
				console.log("Son-----shouldComponentUpdate被调用了",this.props.value,props.value,this.state,state,"true----false")
				return true;
			},
			/*
			    组件即将销毁时调用
			*/
			componentWillUnmount(){
				console.log("Son即将销毁")
			},
			changeProps(){
				this.props.change()
			},
			changeSonProps(){
				this.props.value++;
				this.setState({});
			},
			render(){
				return(
					<div>
						this is props : {this.props.value}
						<br/>
						<button onClick={this.changeProps}>增加父级props值</button>
						<button onClick={this.changeSonProps}>增加自身props值,假修改</button>
					</div>
				)
			}
		})
		
		ReactDOM.render(<Father/>,app);
   </script>
</body>
</html>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风舞红枫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值