Created
June 28, 2015 04:26
-
-
Save RaasAhsan/ec1ec0c169cf94c708dd to your computer and use it in GitHub Desktop.
React router component for applications
This file contains hidden or 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 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