Last active
September 18, 2017 20:40
-
-
Save wking-io/60f007084a9efedd8412da8a69f74f4d to your computer and use it in GitHub Desktop.
Ref callback returning undefined in componentDidMount.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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