Skip to content

Commit cd2087d

Browse files
krawallerryanflorence
authored andcommitted
[added] default handler to routes
1 parent edf684c commit cd2087d

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

modules/components/Route.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var React = require('react');
22
var Configuration = require('../Configuration');
33
var PropTypes = require('../PropTypes');
4-
4+
var RouteHandler = require('./RouteHandler');
55
/**
66
* <Route> components specify components that are rendered to the page when the
77
* URL matches a given pattern.
@@ -39,6 +39,8 @@ var PropTypes = require('../PropTypes');
3939
* );
4040
* }
4141
* });
42+
*
43+
* If no handler is provided for the route, it will render a matched child route.
4244
*/
4345
var Route = React.createClass({
4446

@@ -49,8 +51,14 @@ var Route = React.createClass({
4951
propTypes: {
5052
name: PropTypes.string,
5153
path: PropTypes.string,
52-
handler: PropTypes.func.isRequired,
54+
handler: PropTypes.func,
5355
ignoreScrollBehavior: PropTypes.bool
56+
},
57+
58+
getDefaultProps: function(){
59+
return {
60+
handler: RouteHandler
61+
};
5462
}
5563

5664
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var expect = require('expect');
2+
var React = require('react');
3+
var Router = require('../../index');
4+
var DefaultRoute = require('../DefaultRoute');
5+
var Route = require('../Route');
6+
var { Foo, Bar } = require('../../utils/TestHandlers');
7+
8+
describe('Route', function () {
9+
10+
it('renders default child route if path match but no handler provided', function () {
11+
var routes = (
12+
<Route path='/'>
13+
<Route path='/bar' handler={Bar} />
14+
<DefaultRoute handler={Foo} />
15+
</Route>
16+
);
17+
18+
Router.run(routes, '/', function (App) {
19+
var html = React.renderToString(<App/>);
20+
expect(html).toMatch(/Foo/);
21+
});
22+
});
23+
24+
it('renders matched child route if no handler provided', function () {
25+
var routes = (
26+
<Route path='/'>
27+
<Route path='/bar' handler={Bar} />
28+
<DefaultRoute handler={Foo} />
29+
</Route>
30+
);
31+
32+
Router.run(routes, '/bar', function (App) {
33+
var html = React.renderToString(<App/>);
34+
expect(html).toMatch(/Bar/);
35+
});
36+
});
37+
38+
});

0 commit comments

Comments
 (0)