Skip to content

Commit 205b91a

Browse files
committed
Support removing wrapper element by passing a falsy component.
1 parent b916a62 commit 205b91a

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

lib/Router.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,20 @@ function createRouter(name, component) {
3030
},
3131

3232
render: function() {
33-
// Pass all props except this component to the Router (containing div/body) and the children,
34-
// which are swapped out by the route handler.
35-
var props = assign({}, this.props);
36-
delete props.component;
37-
delete props.children;
38-
delete props.childProps;
39-
4033
// Render the Route's handler.
4134
var handler = this.renderRouteHandler(this.props.childProps);
42-
return React.createElement(this.props.component, props, handler);
35+
36+
if (!this.props.component) {
37+
return handler;
38+
} else {
39+
// Pass all props except this component to the Router (containing div/body) and the children,
40+
// which are swapped out by the route handler.
41+
var props = assign({}, this.props);
42+
delete props.component;
43+
delete props.children;
44+
delete props.childProps;
45+
return React.createElement(this.props.component, props, handler);
46+
}
4347
}
4448
});
4549
}

tests/server/server.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,34 @@ describe('react-router-component (on server)', function() {
8787
});
8888
});
8989

90+
describe('Custom Component', function() {
91+
class AppSection extends React.Component {
92+
render() {
93+
return <Locations className="App" path={this.props.path} component="section">
94+
<Location path="/" handler={<div>mainpage</div>} />
95+
</Locations>;
96+
}
97+
}
98+
99+
class AppNoWrapper extends React.Component {
100+
render() {
101+
return <Locations className="App" path={this.props.path} component={null}>
102+
<Location path="/" handler={<div>mainpage</div>} />
103+
</Locations>;
104+
}
105+
}
106+
107+
it('renders to <section>', function() {
108+
var markup = ReactDOMServer.renderToStaticMarkup(<AppSection path="/" />);
109+
assert.equal(markup, '<section class="App"><div>mainpage</div></section>');
110+
});
111+
112+
it('removes wrapper with falsy value', function() {
113+
var markup = ReactDOMServer.renderToStaticMarkup(<AppNoWrapper path="/" />);
114+
assert.equal(markup, '<div>mainpage</div>');
115+
});
116+
});
117+
90118
describe('contextual router', function() {
91119
class rendersSubSlug extends React.Component {
92120
render() {

0 commit comments

Comments
 (0)