Skip to content

Instantly share code, notes, and snippets.

@jonstuebe
Last active February 21, 2017 16:36
Show Gist options
  • Save jonstuebe/db8368691f5eae8896ac6c463fcb3c74 to your computer and use it in GitHub Desktop.
Save jonstuebe/db8368691f5eae8896ac6c463fcb3c74 to your computer and use it in GitHub Desktop.
Named Routes w/ React Router 4
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import { BrowserRouter as Router } from 'react-router-dom';
import Link from './components/Link';
import { Routes } from './routes';
class App extends Component {
render() {
return (
<Router>
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
<Link name='user_profile' params={{ id: 12 }}>This is a Test</Link>
</div>
<Routes />
</div>
</Router>
);
}
}
export default App;
import React from 'react';
import { Route as PlainRoute } from 'react-router-dom';
const Route = (route) => {
return (
<PlainRoute
path={route.path}
render={props => (
<route.component {...props} routes={route.routes} />
)}
/>
);
}
export default Route;
import React from 'react';
import Route from './components/Route';
import UserProfile from './containers/UserProfile';
export const routes = [
{
name: 'user_profile',
path: '/user/:id',
component: UserProfile
}
];
export const Routes = () => {
return (
<div>
{routes.map((route, i) => (
<Route key={i} {...route} />
))}
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment