Created
March 25, 2016 13:07
-
-
Save toxicFork/81f8eb09aec00d89b084 to your computer and use it in GitHub Desktop.
exposing function to 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 MyComponent extends React.Component{ | |
constructor(props, context) { | |
super(props, context); | |
this.editorControls = null; | |
this.focusFunction = (objectToFocus) => { | |
if(!this.editorControls) { | |
return; | |
} | |
this.editorControls.focus(objectToFocus); | |
}; | |
} | |
componentDidMount() { | |
// create EditorControls here | |
this.editorControls = ...; | |
this.refs.editorControlsContainer.add(this.editorControls); | |
} | |
componentWillUnmount() { | |
this.refs.editorControlsContainer.remove(this.editorControls); | |
// clean up other stuff for this.editorControls | |
this.editorControls = null; | |
} | |
render() { | |
return (<group> | |
<SomeChild/> | |
<SomeOtherChildThatMayLikeFocus focus={this.focusFunction}/> | |
<object3D ref="editorControlsContainer"/> | |
</group>); | |
} | |
} |
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 SomeOtherChildThatMayLikeFocus extends React.Component { | |
static propTypes = { | |
focus: React.PropTypes.func.isRequired, | |
}; | |
componentDidMount() { | |
setTimeout(() => { | |
this.props.focus(this.refs.mesh); | |
// focus mesh in 5 seconds | |
}, 5000); | |
} | |
render() { | |
return (<mesh ref="mesh"/>); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment