Skip to content

Commit 32212a1

Browse files
committed
feat: support subroutes with react-router
1 parent 186e8fe commit 32212a1

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react';
2+
import { Route } from 'react-router';
3+
4+
const RouteWithSubRoutes = props => {
5+
const {
6+
path,
7+
computedMatch,
8+
component: Component,
9+
routes,
10+
restProps
11+
} = props;
12+
13+
return (
14+
<Route
15+
path={path}
16+
render={props => {
17+
// pass the sub-routes down to keep nesting
18+
return (
19+
<Component
20+
{...props}
21+
{...restProps}
22+
match={computedMatch}
23+
routes={routes}
24+
/>
25+
);
26+
}}
27+
/>
28+
);
29+
};
30+
31+
export default RouteWithSubRoutes;

common/js/components/common/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { default as ErrorPage } from './ErrorPage';
22
export { default as Footer } from './Footer';
33
export { default as Header } from './Header';
4+
export { default as RouteWithSubRoutes } from './RouteWithSubRoutes';

common/js/containers/App/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
2-
import { Switch, Route } from 'react-router-dom';
2+
import { Switch } from 'react-router-dom';
3+
import { RouteWithSubRoutes } from 'components/common';
34
import { Container } from 'semantic-ui-react';
45
import { Header, Footer } from 'components/common';
56
import routes from 'routes';
@@ -8,7 +9,9 @@ const App = () => (
89
<Container fluid={false}>
910
<Header />
1011
<Switch>
11-
{routes.map(route => <Route key={route.path} {...route} />)}
12+
{routes.map(route => (
13+
<RouteWithSubRoutes key={route.path} {...route} />
14+
))}
1215
</Switch>
1316
<Footer />
1417
</Container>

0 commit comments

Comments
 (0)