Skip to content

Instantly share code, notes, and snippets.

@wking-io
Last active September 18, 2017 20:40
Show Gist options
  • Save wking-io/60f007084a9efedd8412da8a69f74f4d to your computer and use it in GitHub Desktop.
Save wking-io/60f007084a9efedd8412da8a69f74f4d to your computer and use it in GitHub Desktop.
Ref callback returning undefined in 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} />
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment