Skip to content

Commit e1bf3b1

Browse files
committed
move named components back onto props.children
This makes building abstractions like AsyncProps simpler and less error- prone because it knows exactly where to find the elements it needs to clone or wrap or whatever. Having them on `props` makes it impossible to know if the component there is a component from a route or somewhere else. This was the original API, but we moved them off of children because it seemed a little to weird, but lately passing a function to children has been accepted, so why not objects?
1 parent 94509e7 commit e1bf3b1

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

examples/sidebar/app.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,14 @@ var IndexSidebar = React.createClass({
8181

8282
var App = React.createClass({
8383
render() {
84+
var { children } = this.props;
8485
return (
8586
<div>
8687
<div className="Sidebar">
87-
{this.props.sidebar || <IndexSidebar />}
88+
{children ? children.sidebar : <IndexSidebar />}
8889
</div>
8990
<div className="Content">
90-
{this.props.content || <Index />}
91+
{children ? children.content : <Index />}
9192
</div>
9293
</div>
9394
);

modules/RoutingContext.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,8 @@ var RoutingContext = React.createClass({
6060
routeParams
6161
};
6262

63-
if (React.isValidElement(element)) {
63+
if (element)
6464
props.children = element;
65-
} else if (element) {
66-
// In render, do var { header, sidebar } = this.props;
67-
Object.assign(props, element);
68-
}
6965

7066
if (typeof components === 'object') {
7167
var elements = {};

modules/__tests__/Router-test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,41 @@ describe('Router', function () {
104104
});
105105
});
106106

107+
describe('with named components', function() {
108+
it('renders the named components', function(done) {
109+
var Parent = React.createClass({
110+
render() {
111+
return (
112+
<div>
113+
{this.props.children.sidebar}-{this.props.children.content}
114+
</div>
115+
);
116+
}
117+
});
118+
119+
var Sidebar = React.createClass({
120+
render() {
121+
return <div>sidebar</div>;
122+
}
123+
});
124+
125+
var Content = React.createClass({
126+
render() {
127+
return <div>content</div>;
128+
}
129+
});
130+
131+
React.render((
132+
<Router history={createHistory('/')}>
133+
<Route component={Parent}>
134+
<Route path="/" components={{sidebar: Sidebar, content: Content}} />
135+
</Route>
136+
</Router>
137+
), node, function () {
138+
expect(node.textContent.trim()).toEqual('sidebar-content');
139+
done();
140+
});
141+
});
142+
});
143+
107144
});

0 commit comments

Comments
 (0)