Last active
May 23, 2017 19:32
-
-
Save ryanflorence/6cb1e2a3fbd31569ece8776cf0fc2944 to your computer and use it in GitHub Desktop.
This file contains 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
// makeRouteComponent.js | |
import { compile } from 'path-to-regexp' | |
import { Route } from 'react-router' | |
export const makeRouteComponent = config => Comp => { | |
Comp.__toPath = compile(config.path) | |
return <Route {...config} component={Comp}/> | |
} | |
export const pathTo = (Comp, params) => ( | |
Comp.__toPath(params) | |
) | |
//////////////////////////////////////////////////////////// | |
// Dashboard.js | |
const Dashboard = () => <div>Dash!</div> | |
export default makeRouteComponent({ path: '/dashboard' }) | |
//////////////////////////////////////////////////////////// | |
// Somehwere else in render | |
import { render } from 'react-dom' | |
import { BrowserRouter } from 'react-router' | |
import { pathTo } from './makeRouteComponent' | |
import Dashboard from './Dashboard' | |
import Inbox from './Inbox' | |
render(( | |
<BrowserRouter> | |
<div> | |
<ul> | |
<li><Link to={pathTo(Dashboard)}>Dashboard</Link></li> | |
<li><Link to={pathTo(Inbox)}>Inbox</Link></li> | |
</ul> | |
<main> | |
<Dashboard/> | |
<Inbox/> | |
</main> | |
</div> | |
</BrowserRouter> | |
), root) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment