Skip to content

Commit 84056ba

Browse files
committed
[fixed] Ignore falsy routes
Routes which are null/undefined should be skipped in the React.Children traversal when creating routes. This makes the Router's behavior closer match React in handling empty elements.
1 parent 5fa86d5 commit 84056ba

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

tests/Router-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,20 @@ describe('Router.run', function () {
832832
});
833833
});
834834

835+
it('does not break on falsy routes', function (done) {
836+
var routes = [
837+
<Route handler={Foo} path="/foo"/>,
838+
null,
839+
<Route handler={Bar} path="/bar"/>,
840+
undefined
841+
];
842+
Router.run(routes, '/foo', function (Handler, state) {
843+
var html = React.renderToString(<Handler/>);
844+
expect(html).toMatch(/Foo/);
845+
done();
846+
});
847+
});
848+
835849
it('matches nested routes', function (done) {
836850
var routes = (
837851
<Route handler={Nested} path='/'>

utils/createRoutesFromChildren.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ function createRoutesFromChildren(children, parentRoute, namedRoutes) {
154154
var routes = [];
155155

156156
React.Children.forEach(children, function (child) {
157-
// Exclude <DefaultRoute>s and <NotFoundRoute>s.
158-
if (child = createRoute(child, parentRoute, namedRoutes))
157+
// Exclude null values, <DefaultRoute>s and <NotFoundRoute>s.
158+
if (child && (child = createRoute(child, parentRoute, namedRoutes)))
159159
routes.push(child);
160160
});
161161

0 commit comments

Comments
 (0)