Skip to content

Instantly share code, notes, and snippets.

@diegocasmo
Created February 20, 2017 15:25
Show Gist options
  • Save diegocasmo/357f8463bd4b217a2a7443326f3e0942 to your computer and use it in GitHub Desktop.
Save diegocasmo/357f8463bd4b217a2a7443326f3e0942 to your computer and use it in GitHub Desktop.
The App component is the top level component of the application and all other components are wrapped by it.
class App extends React.Component {
// Encapsulate 'appData' context as props for all children
renderChildrenWithContextAsProps() {
return React.Children.map(this.props.children, (child) => {
return React.cloneElement(child, {
appData: this.context.appData
});
});
};
render() {
return (
<div>
{this.renderChildrenWithContextAsProps()}
</div>
);
}
}
App.contextTypes = {
appData: React.PropTypes.object.isRequired
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment