Created
March 22, 2018 08:26
-
-
Save dlueth/e63442562d269d5308179fbeb4e4133c to your computer and use it in GitHub Desktop.
[React: parent/child reference] #react
This file contains hidden or 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
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; |
This file contains hidden or 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
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