Skip to content

Instantly share code, notes, and snippets.

@olov
Last active September 13, 2016 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olov/948b69f73d46e5b59c517681292bd908 to your computer and use it in GitHub Desktop.
Save olov/948b69f73d46e5b59c517681292bd908 to your computer and use it in GitHub Desktop.
import { Router } from 'react-router';
import { Match } from "./matchchildren"
const App = () => (
<Router>
<Match pattern="/"><Hello name="Yoyoma"/></Match>
</Router>
)
const Hello = (props) => {
return <div>
Hello {props.name}<br/><br/>
[props.pathname is {props.pathname}]
</div>;
}
import { Match: OldMatch } from 'react-router';
// assuming ES6, no object spread
// babel
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
export function Match(props) {
const children = props.children;
if (!children) {
return React.createElement(OldMatch, props, null);
}
if (props.render) {
throw new Error("render or children, choose one")
}
const newprops = _objectWithoutProperties(props, ["children"]);
newprops.render = (renderprops) => React.cloneElement(children, renderprops);
return React.createElement(OldMatch, newprops, null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment