From 84056ba0ecad4db1b95d0d5a196fbaf1438afb90 Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Mon, 19 Jan 2015 20:05:55 -0500 Subject: [PATCH] [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. --- tests/Router-test.js | 14 ++++++++++++++ utils/createRoutesFromChildren.js | 4 ++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/Router-test.js b/tests/Router-test.js index 97e1c718b8..95574a0679 100644 --- a/tests/Router-test.js +++ b/tests/Router-test.js @@ -832,6 +832,20 @@ describe('Router.run', function () { }); }); + it('does not break on falsy routes', function (done) { + var routes = [ + , + null, + , + undefined + ]; + Router.run(routes, '/foo', function (Handler, state) { + var html = React.renderToString(); + expect(html).toMatch(/Foo/); + done(); + }); + }); + it('matches nested routes', function (done) { var routes = ( diff --git a/utils/createRoutesFromChildren.js b/utils/createRoutesFromChildren.js index 9e50c85b90..5e74816c6a 100644 --- a/utils/createRoutesFromChildren.js +++ b/utils/createRoutesFromChildren.js @@ -154,8 +154,8 @@ function createRoutesFromChildren(children, parentRoute, namedRoutes) { var routes = []; React.Children.forEach(children, function (child) { - // Exclude s and s. - if (child = createRoute(child, parentRoute, namedRoutes)) + // Exclude null values, s and s. + if (child && (child = createRoute(child, parentRoute, namedRoutes))) routes.push(child); });