Skip to content

Instantly share code, notes, and snippets.

@dlueth
Created March 22, 2018 08:26
Show Gist options
  • Save dlueth/e63442562d269d5308179fbeb4e4133c to your computer and use it in GitHub Desktop.
Save dlueth/e63442562d269d5308179fbeb4e4133c to your computer and use it in GitHub Desktop.
[React: parent/child reference] #react
class Child extends Component {
static defaultProps = {
onRef: undefined
}
componentDidMount() {
this.props.onRef(this);
}
componentWillUnmount() {
this.props.onRef(undefined);
}
render() {
}
}
Child.propTypes = {
onRef: PropTypes.func
};
export default Child;
class Parent extends Component {
componentDidMount() {
if(this.child) {
this.child.setState(/* ... */);
}
}
render() {
return (<Fragment><Child onRef={(ref) => { this.child = ref; }} /></Fragment>);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment