0
所以我試圖用裁判回調格局這裏解釋:https://facebook.github.io/react/docs/refs-and-the-dom.html,而是繼續得到了一個未定義的家長componentDidMount方法陣營裁判回調格局返回父componentDidMount()未定義
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} />
);
}
}
將一個'console.log'添加到'componentDidMount',另一個添加到'getChildRef',看哪個先登錄。 – bennygenel