Created
January 17, 2018 16:54
-
-
Save squeedee/288c677f990dbe0aa052536a243fc92b to your computer and use it in GitHub Desktop.
Simple props based react router
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
<Router authorized={true}> | |
<Match fn={(props) => (props.authorized)}> | |
<Environments/> | |
</Match> | |
<Match fn={(props) => (!props.authorized)}> | |
<SlackPage/> | |
</Match> | |
</Router> |
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
export const Match = ({children}) => (<React.Fragment>{children}</React.Fragment>) | |
class Router extends Component { | |
render() { | |
let newChildren = [] | |
this.props.children.forEach( | |
(child) => { | |
if (child.props.fn(this.props)) { | |
newChildren.push(child) | |
} | |
} | |
) | |
return (<React.Fragment>{newChildren}</React.Fragment>) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment