Last active
September 13, 2016 20:53
-
-
Save olov/948b69f73d46e5b59c517681292bd908 to your computer and use it in GitHub Desktop.
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 { 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>; | |
} |
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 { 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