Antd在父组件中调用子组件的方法

在使用ant design的时候,在父组件中使用子组件的方法、props、state、context:

对子组件使用 wrappedComponentRef 属性,如下所示

//父组件:
test=()=>{
	//打印出子组件MTLI的属性
	console.log(this.MTLIRef);
}
formCheck=(param)=>{
	//打印出参数
	console.log(param)
}
render(){
	return(
		//某个子组件
		<MTLI
		   	wrappedComponentRef={ref=> {
		     this.MTLIRef = ref;
		   	}}
		   	formCheck={this.formCheck}
		 />
	)
}

//MTLI组件:
render(){
	return(
		//子组件中调用父组件的方法并传参
		<div onClick={e=>{this.props.formCheck(param)}}></div>
	)
}

如有错误,欢迎指正,谢谢!
原网址:https://www.cnblogs.com/wyangnb/p/9400347.html

猜你喜欢

转载自blog.csdn.net/qq_42999924/article/details/95306396