1.react中常用props实现子组件数据到父组件的传递
2.父组件调用子组件的方法
import React, {Component} from 'react';
export default class MatchAgency extends Component {
{
render() {
return(
<div>
<CaptchaLxjl onRef={this.clearref} />
<button onClick={this.submit} >提交</button>
</div>
)
}
clearref = (ref) => {
this.child = ref
}
submit = (e) => {
this.child.clear()
}
}
//子组件
class CaptchaLxjl extends Component {
componentDidMount(){
//必须在这里声明,所以 ref 回调可以引用它
this.props.onRef(this)
}
clear = () => alert('清空数据')
render() {
return (<div>子组件数据</div>)
}
}