我在反应中使用裁判。我有一个父组件“ UserEducation.js”和一个子组件“ EducationDetailsContainer”
我在调用子组件时在父组件中传递onRef。但是每次获取引用都是不确定的。
我的父组件中的代码
<CollapsibleComponentUserProfile
collapsibleTitle="Education"
showAddButtow={false}
onSaveClick={this.checkValidationsOnSave}
>
{/* <EducationEdit */}
<EducationDetailsContainer (my child component)
onRef={ref => (this.educationReference = ref)}
{...{
rightHalf,
classes,
year,
month,
dropdown,
leftHalf,
fullWidth,
allDropDownWidth,
}}
// onRef={ref => this.dummyFunction(ref)}
/>
{console.log('hint2', this.educationReference)}
</CollapsibleComponentUserProfile>
checkValidationsOnSave = e => {
debugger;
this.educationReference.checkForErrors();
};
这是我子组件中的组件生命周期
componentDidMount() {
this.props.onRef(this);
}
// componentWillUnmount() {
// this.props.onRef(null);
// }
componentWillReceiveProps(nextProps) {
this.props.onRef(this);
}
我已经在我的父级组件中声明了我的educationReference状态
educationReference: '',
}
内部构造函数
this.educationReference = React.createRef();