Last active
August 29, 2015 14:16
-
-
Save varoot/d62b410e968368c527b6 to your computer and use it in GitHub Desktop.
How to use react-router as a component (when server rendering only support rendering of components)
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
var ReactRouter = require('react-router'); | |
var AppRouter = React.createClass({ | |
propTypes: { | |
originalUrl: React.PropTypes.string | |
}, | |
getInitialState: function() { | |
return { output: (<span>Routing Error</span>) }; | |
}, | |
componentWillMount: function() { | |
var _this = this; | |
// check document to render differently on server & client | |
if (typeof(document) == 'object') { | |
url = ReactRouter.HistoryLocation | |
} else { | |
url = this.props.originalUrl | |
} | |
ReactRouter.run(routes, url, function(Handler) { | |
_this.setState({ output: <Handler /> }); | |
}); | |
}, | |
render: function() { | |
return this.state.output; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment