Skip to content

Instantly share code, notes, and snippets.

@RaasAhsan
Created June 28, 2015 04:26
Show Gist options
  • Save RaasAhsan/ec1ec0c169cf94c708dd to your computer and use it in GitHub Desktop.
Save RaasAhsan/ec1ec0c169cf94c708dd to your computer and use it in GitHub Desktop.
React router component for applications
import React from 'react';
class Router extends React.Component {
constructor(props) {
super(props);
this.state = {
routes: props.routes,
path: "index",
params: {}
};
}
getChildContext() {
return {
router: {
routeTo: (path, params) => {
this.routeTo(path, params);
},
currentRoute: () => {
return this.state.path;
}
}
};
}
routeTo(path, params) {
if(this.state.path != path || this.state.path != params) {
this.setState({path: path, params: params});
}
}
render() {
let Handler = this.props.handler;
let Route = this.state.routes[this.state.path];
return (
<Handler>
<Route {...this.props.params}/>
</Handler>
);
}
}
Router.propTypes = {
handler: React.PropTypes.func
}
Router.childContextTypes = {
router: React.PropTypes.object
}
import Link from './RouterLink';
Router.Link = Link;
Router.Navigation = {
contextTypes: {
router: React.PropTypes.object
}
}
module.exports = Router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment