Last active
October 6, 2015 03:23
-
-
Save bceskavich/18bd36052366b074783c to your computer and use it in GitHub Desktop.
A quick utility to pass React props down a react-router chain in react-router v.1.0.0+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import omit from 'lodash/object/omit'; | |
export default function renderRouteChildren(props) { | |
const { children } = props; | |
return children && React.cloneElement(children, {...omit(props, 'children')}); | |
} | |
// EXAMPLE | |
class App extends React.Component { | |
render() { | |
return <div>{renderRouteChildren(this.props)}</div>; | |
} | |
} | |
// vs. WITHOUT - props aren't passed down :( | |
class App extends React.Component { | |
render() { | |
return <div>{this.props.children}</div>; | |
} | |
} | |
// vs. WITHOUT - props needs to explicitly defined to pass down :( | |
// vs. WITHOUT - props aren't passed down :( | |
class App extends React.Component { | |
render() { | |
return ( | |
<div> | |
{this.props.children && React.cloneElement(this.props.children, { | |
prop1: this.props.prop1, | |
prop2: this.props.prop2 | |
})} | |
</div> | |
); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment