Skip to content

Instantly share code, notes, and snippets.

@toxicFork
Created March 25, 2016 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toxicFork/81f8eb09aec00d89b084 to your computer and use it in GitHub Desktop.
Save toxicFork/81f8eb09aec00d89b084 to your computer and use it in GitHub Desktop.
exposing function to child
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>);
}
}
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