Skip to content

Instantly share code, notes, and snippets.

@srph
Last active March 26, 2019 18:45
Show Gist options
  • Save srph/9b0810b9c58aedeca9df154ce3ad2808 to your computer and use it in GitHub Desktop.
Save srph/9b0810b9c58aedeca9df154ce3ad2808 to your computer and use it in GitHub Desktop.
React Router 4: Allow nested routes

This component allows us to do this:

<Switch>
  <Route>
    <Switch>
      <Route />
      <Route />
      <Route />
      <Route />
    </Switch>
  </Route>
</Switch>
import * as React from 'react'
import { Route } from 'react-router-dom'
import { RouteProps } from 'react-router'
class RouterRoute extends React.Component<RouteProps> {
render() {
const { component, children, ...rest } = this.props
const Component = this.props.component
return (
<Route {...rest} render={(props: RouteProps) => <Component {...props} {...rest}>{children}</Component>} />
)
}
}
export default RouterRoute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment