Yes, you can create a custom Route component. This is a kind of hacking because the jet router v3 is hacked, but it is possible. Here is an example:
import React from 'react'; import { IndexRoute, Route } from 'react-router'; import { createRoutesFromReactChildren } from 'react-router/lib//RouteUtils'; const CrudRoute = () => <div><CrudRoute> elements are for configuration only and should not be rendered</div>; CrudRoute.createRouteFromReactElement = (element, parentRoute) => { const { path, list, edit, create, remove } = element.props; // dynamically add crud routes const crudRoute = createRoutesFromReactChildren( <Route path={path}> <IndexRoute component={list} /> <Route path="create" component={create} /> <Route path=":id" component={edit} /> <Route path=":id/remove" component={remove} /> </Route>, parentRoute )[0]; // higher-order component to pass path as resource to components crudRoute.component = ({ children }) => ( <div> {React.Children.map(children, child => React.cloneElement(child, { path }))} </div> ); return crudRoute; }; export default CrudRoute;
Use this custom route as follows:
import CrudRoute from './CrudRoute'; <Router history={history}> <CrudRoute path="posts" list={PostList} create={PostCreate} edit={PostEdit} remove={PostRemove} /> <CrudRoute path="comments" list={CommentList} create={CommentCreate} edit={CommentEdit} remove={CommentRemove} /> </Router>
Read more about this method here: http://marmelab.com/blog/2016/09/20/custom-react-router-component.html
React-router v4 will make it extremely simple, since the fake <Route> component disappears and leaves the path to the true React <Match> component.
source share