Created
August 27, 2015 22:20
-
-
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
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
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