0
Also ich versuche, die ref Rückruf Muster hier erklärt zu verwenden: https://facebook.github.io/react/docs/refs-and-the-dom.html, aber weiterhin in der componentDidMount Parent MethodeReagieren ref Rückruf Muster in Parent componentDidMount undefined zurückgibt()
const Child = ({ createFormHeight, getChildRef }) => (
<CreateForm createFormHeight={createFormHeight}>
// getting reference from here
<div ref={getChildRef}>
// form markup here
</div>
</CreateForm>
);
class Parent extends Component {
constructor(props) {
super(props);
this.state = {
createFormHeight: '',
}
}
componentDidMount() {
const createFormHeight = this.formWrapper.offsetHeight; // returns
error that this.formWrapper is undefined
this.setFormHeight(createFormHeight);
}
getChildRef = (el) => {
// if I console.log(el) it returns the div here
this.formWrapper = el;
}
setFormHeight = (height) => {
this.setState(() => ({ createFormHeight: height }));
};
render() {
const { createIsOpen, createFormHeight } = this.state;
return (
<CreateMember createFormHeight={createFormHeight} getChildRef=
{this.getChildRef} />
);
}
}
fügen Sie eine 'console.log' zu' componentDidMount' und eine zu 'getChildRef' hinzu, um zu sehen, welcher zuerst loggt. – bennygenel