The main API into react router. It runs your routes, matching them against a location, and then calls back with the next state for you to render.
Router.run(routes, [location,] callback)
Your route config. See Route
Defaults to Router.HashLocation
. If given a Location
object, it
will setup and listen for changes on it, if given a string path, the
router will immediately match that path and callback.
// Defaults to `Router.HashLocation`
// callback is called whenever the hash changes
Router.run(routes, callback);
// HTML5 History
// callback is called when history events happen
Router.run(routes, Router.HistoryLocation, callback);
// Server rendering
// callback is called once, immediately.
Router.run(routes, '/some/path', callback);
The callback receives two arguments:
Handler
state
A ReactComponent class with the current match all wrapped up inside it, ready for you to render.
An object containing the matched state.
The current URL path with query string.
The action that lead to the route change.
The current URL path without the query string.
The active params in the location match.
The active query in the location match.
An array of the matched routes. Very useful for fetching data before rendering.
See also: Route.
Basic Usage:
Router.run(routes, function (Handler) {
// whenever the url changes, this callback is called again
React.render(<Handler/>, document.body);
});
Sample data fetching using state.routes
. Check out the
async-data example.
var resolveHash = require('when/keys').all;
var SampleHandler = React.createClass({
statics: {
// this is going to be called in the `run` callback
fetchData: function (params) {
return fetchStuff(params);
}
},
// ...
});
Router.run(routes, Router.HistoryLocation, function (Handler, state) {
// create the promises hash
var promises = state.routes.filter(function (route) {
// gather up the handlers that have a static `fetchData` method
return route.handler.fetchData;
}).reduce(function (promises, route) {
// reduce to a hash of `key:promise`
promises[route.name] = route.handler.fetchData(state.params);
return promises;
}, {});
resolveHash(promises).then(function (data) {
// wait until we have data to render, the old screen stays up until
// we render
React.render(<Handler data={data}/>, document.body);
});
});
Server Rendering
something.serve(function (req, res) {
Router.run(routes, req.path, function (Handler, state) {
// could fetch data like in the previous example
fetchData(state.matches).then(function (data) {
var html = React.renderToString(<Handler data={data} />);
res.send(html);
});
});
});