Skip to content

Instantly share code, notes, and snippets.

@mars
Created August 27, 2015 22:20
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 mars/2f70f79066fa78f33801 to your computer and use it in GitHub Desktop.
Save mars/2f70f79066fa78f33801 to your computer and use it in GitHub Desktop.
Pass the `router` instance down to Route children; React 0.13.3, React Router 1.0.0beta-4, ES2015, ES6
import React from 'react';
// Top-level Route component receives the router in context and passes it onto each child's props
export default class AppView extends React.Component {
render() {
return (
<div className="container">
{
// Add `props.router` to each route component
React.Children.map(this.props.children, c => {
return React.cloneElement(c, { ...c.props, router: this.context.router });
})
}
</div>
);
}
}
AppView.contextTypes = {
router: React.PropTypes.object.isRequired
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment