Skip to content

Instantly share code, notes, and snippets.

@bceskavich
Last active October 6, 2015 03:23
Show Gist options
  • Save bceskavich/18bd36052366b074783c to your computer and use it in GitHub Desktop.
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+
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