react 组件实例更新之函数调用顺序

废话不多说  直接上代码

class InputTest extends Com{
	constructor(props){
		super(props);
		this.state = {
			value:""
		}
		this.onChangeHandle = this.onChangeHandle.bind(this);
	}
	onChangeHandle(e){
		this.setState({
			value:"okkkk!"
		});
		setTimeout(function(){
			console.log('settimeout called!');
		},0);
		console.log('onchangehandle called');
	}
	componentDidMount(){
		console.log('componentdidmount!');
	}
	componentDidUpdate(){
		console.log('componentdidupdata');
	}
	render(){
		console.log('render call');
		
		return (
			<div>
				<button type = "button" onClick={this.onChangeHandle} >click!</button>
				ok{this.state.value}
			</div>
		);
	}
	
}

打印顺序

组件实例加载完成后

控制台显示信息

render call   
componentdidmount!

先调用render函数,当组件加载完成后调用componentDidMount函数

当然我要说的问题不是这个而是,而是点击button按钮 调用函数之后onChangeHandle

控制台显示信息

onchangehandle called
 render call
 componentdidupdata
 settimeout called!

onChangeHandle函数调用 并调用了setState函数,然后函数会继续执行完毕,但是在onChangeHandle设置了一个定时器,此时定时器不会立即执行,而是会等到组件实例更新完毕之后再执行改定时器设置的函数。





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值