Skip to content

Instantly share code, notes, and snippets.

@srph
Created November 16, 2018 14:58
Show Gist options
  • Save srph/01f8ee068964ff882be3f717c5173b83 to your computer and use it in GitHub Desktop.
Save srph/01f8ee068964ff882be3f717c5173b83 to your computer and use it in GitHub Desktop.
React Router v4: Passthrough props for Switch
import * as React from 'react'
import { Switch } from 'react-router-dom'
interface IRouterSwitch {
children: any
}
class RouterSwitch extends React.Component<IRouterSwitch, {}> {
render() {
const {children, ...rest} = this.props
console.log('RouterSwitch', rest, children)
return (
<Switch>
{React.Children.map(children, (child: any) =>
React.cloneElement(child, rest)
)}
</Switch>
)
}
}
export default RouterSwitch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment