Skip to content

Commit 5af49d4

Browse files
sophiebitsryanflorence
authored andcommitted
[changed] Split <Routes> component from <Route>
Fixes #112. Test Plan: Loaded each of the examples and clicked around. npm test too.
1 parent 6bfcdfc commit 5af49d4

File tree

17 files changed

+613
-510
lines changed

17 files changed

+613
-510
lines changed

README.md

+43-25
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,38 @@ Usage
6666
var Route = require('react-router').Route;
6767
6868
React.renderComponent((
69-
<Route handler={App}>
70-
<Route name="about" handler={About}/>
71-
<Route name="users" handler={Users}>
72-
<Route name="user" path="/user/:userId" handler={User}/>
69+
<Routes>
70+
<Route handler={App}>
71+
<Route name="about" handler={About}/>
72+
<Route name="users" handler={Users}>
73+
<Route name="user" path="/user/:userId" handler={User}/>
74+
</Route>
7375
</Route>
74-
</Route>
76+
</Routes>
7577
), document.body);
7678
```
7779

7880
Or if JSX isn't your jam:
7981

8082
```js
8183
React.renderComponent((
82-
Route({handler: App},
83-
Route({name: "about", handler: About}),
84-
Route({name: "users", handler: Users},
85-
Route({name: "user", path: "/user/:userId", handler: User})
84+
Routes({},
85+
Route({handler: App},
86+
Route({name: "about", handler: About}),
87+
Route({name: "users", handler: Users},
88+
Route({name: "user", path: "/user/:userId", handler: User})
89+
)
8690
)
8791
)
8892
), document.body);
8993
```
9094

91-
- Urls will be matched to the deepest route, and then all the routes up
95+
- URLs will be matched to the deepest route, and then all the routes up
9296
the hierarchy are activated and their "handlers" (normal React
9397
components) will be rendered.
9498

99+
- Paths are assumed from names unless specified.
100+
95101
- Each handler will receive a `params` property containing the matched
96102
parameters form the url, like `:userId`.
97103

@@ -194,24 +200,33 @@ Related Modules
194200
API
195201
---
196202

203+
### Routes (component)
204+
205+
Configuration component for your router, all `<Route/>`s must be
206+
children of a `<Routes/>`. It is the component you provide to
207+
`React.renderComponent(routes, el)`.
208+
209+
#### Props
210+
211+
**location** - `"hash"` or `"history"`, defaults to `"hash"`. Configures
212+
what type of url you want, hash includes `#/` in the url and works
213+
without a server, if you use `history` your server will need to support
214+
it.
215+
197216
### Route (component)
198217

199218
Configuration component to declare your application's routes and view hierarchy.
200219

201220
#### Props
202221

203-
**location** - The method to use for page navigation when initializing the router.
204-
May be either "hash" to use URLs with hashes in them and the `hashchange` event or
205-
"history" to use the HTML5 history API. This prop is only ever used on the root
206-
route that is rendered into the page. The default is "hash".
207-
208222
**name** - The name of the route, used in the `Link` component and the
209223
router's transition methods.
210224

211225
**path** - The path used in the URL, supporting dynamic segments. If
212-
left undefined, the path will be defined by the `name`. This path is always
213-
absolute from the URL "root", even if the leading slash is left off. Nested
214-
routes do not inherit the path of their parent.
226+
left undefined, the path will be defined by the `name`, and if there is
227+
no name, will default to `/`. This path is always absolute from the URL
228+
"root", even if the leading slash is left off. Nested routes do not
229+
inherit the path of their parent.
215230

216231
**handler** - The component to be rendered when the route matches.
217232

@@ -225,14 +240,17 @@ passing in any additional props as needed.
225240
#### Examples
226241

227242
```xml
228-
<Route handler={App}>
229-
<!-- path is automatically assigned to the name since it is omitted -->
230-
<Route name="about" handler={About}/>
231-
<Route name="users" handler={Users}>
232-
<!-- note the dynamic segment in the path -->
233-
<Route name="user" handler={User} path="/user/:id"/>
243+
<Routes>
244+
<!-- path defaults to '/' since no name or path provided -->
245+
<Route handler={App}>
246+
<!-- path is automatically assigned to the name since it is omitted -->
247+
<Route name="about" handler={About}/>
248+
<Route name="users" handler={Users}>
249+
<!-- note the dynamic segment in the path -->
250+
<Route name="user" handler={User} path="/user/:id"/>
251+
</Route>
234252
</Route>
235-
</Route>
253+
</Routes>
236254
```
237255

238256
Or w/o JSX:

examples/auth-flow/app.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var React = require('react');
33
var Router = require('../../modules/main');
44
var Route = Router.Route;
5+
var Routes = Router.Routes;
56
var Link = Router.Link;
67

78
var App = React.createClass({
@@ -179,12 +180,14 @@ function pretendRequest(email, pass, cb) {
179180

180181

181182
var routes = (
182-
<Route handler={App}>
183-
<Route name="login" handler={Login}/>
184-
<Route name="logout" handler={Logout}/>
185-
<Route name="about" handler={About}/>
186-
<Route name="dashboard" handler={Dashboard}/>
187-
</Route>
183+
<Routes>
184+
<Route handler={App}>
185+
<Route name="login" handler={Login}/>
186+
<Route name="logout" handler={Logout}/>
187+
<Route name="about" handler={About}/>
188+
<Route name="dashboard" handler={Dashboard}/>
189+
</Route>
190+
</Routes>
188191
);
189192

190193
React.renderComponent(routes, document.body);

examples/data-flow/app.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var React = require('react');
33
var Router = require('../../modules/main');
44
var Route = Router.Route;
5+
var Routes = Router.Routes;
56
var Link = Router.Link;
67

78
var App = React.createClass({
@@ -64,9 +65,11 @@ var Taco = React.createClass({
6465
});
6566

6667
var routes = (
67-
<Route handler={App}>
68-
<Route name="taco" path="taco/:name" handler={Taco}/>
69-
</Route>
68+
<Routes>
69+
<Route handler={App}>
70+
<Route name="taco" path="taco/:name" handler={Taco}/>
71+
</Route>
72+
</Routes>
7073
);
7174

7275
React.renderComponent(routes, document.body);

examples/dynamic-segments/app.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var React = require('react');
33
var Router = require('../../modules/main');
44
var Route = Router.Route;
5+
var Routes = Router.Routes;
56
var Link = Router.Link;
67

78
var App = React.createClass({
@@ -45,11 +46,13 @@ var Task = React.createClass({
4546
});
4647

4748
var routes = (
48-
<Route handler={App}>
49-
<Route name="user" path="/user/:userId" handler={User}>
50-
<Route name="task" path="/user/:userId/tasks/:taskId" handler={Task}/>
49+
<Routes>
50+
<Route handler={App}>
51+
<Route name="user" path="/user/:userId" handler={User}>
52+
<Route name="task" path="/user/:userId/tasks/:taskId" handler={Task}/>
53+
</Route>
5154
</Route>
52-
</Route>
55+
</Routes>
5356
);
5457

5558
React.renderComponent(routes, document.body);

examples/master-detail/app.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var React = require('react');
33
var Router = require('../../modules/main');
44
var Route = Router.Route;
5+
var Routes = Router.Routes;
56
var Link = Router.Link;
67

78
var api = 'http://addressbook-api.herokuapp.com/contacts';
@@ -203,11 +204,13 @@ var NotFound = React.createClass({
203204
});
204205

205206
var routes = (
206-
<Route handler={App}>
207-
<Route name="new" path="contact/new" handler={NewContact}/>
208-
<Route name="not-found" path="contact/not-found" handler={NotFound}/>
209-
<Route name="contact" path="contact/:id" handler={Contact}/>
210-
</Route>
207+
<Routes>
208+
<Route handler={App}>
209+
<Route name="new" path="contact/new" handler={NewContact}/>
210+
<Route name="not-found" path="contact/not-found" handler={NotFound}/>
211+
<Route name="contact" path="contact/:id" handler={Contact}/>
212+
</Route>
213+
</Routes>
211214
);
212215

213216
React.renderComponent(routes, document.body);

examples/partial-app-loading/app.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var React = require('react');
33
var Router = require('../../modules/main');
44
var Route = Router.Route;
5+
var Routes = Router.Routes;
56
var Link = Router.Link;
67

78
var AsyncReactComponent = {
@@ -59,11 +60,13 @@ var App = React.createClass({
5960
});
6061

6162
var routes = (
62-
<Route handler={App}>
63-
<Route name="dashboard" path="dashboard" handler={PreDashboard}>
64-
<Route name="inbox" path="dashboard/inbox" handler={PreInbox}/>
63+
<Routes>
64+
<Route handler={App}>
65+
<Route name="dashboard" path="dashboard" handler={PreDashboard}>
66+
<Route name="inbox" path="dashboard/inbox" handler={PreInbox}/>
67+
</Route>
6568
</Route>
66-
</Route>
69+
</Routes>
6770
);
6871

6972
React.renderComponent(routes, document.body);

examples/query-params/app.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var React = require('react');
33
var Router = require('../../modules/main');
44
var Route = Router.Route;
5+
var Routes = Router.Routes;
56
var Link = Router.Link;
67

78
var App = React.createClass({
@@ -32,9 +33,11 @@ var User = React.createClass({
3233
});
3334

3435
var routes = (
35-
<Route handler={App}>
36-
<Route name="user" path="user/:userId" handler={User}/>
37-
</Route>
36+
<Routes>
37+
<Route handler={App}>
38+
<Route name="user" path="user/:userId" handler={User}/>
39+
</Route>
40+
</Routes>
3841
);
3942

4043
React.renderComponent(routes, document.body);

examples/shared-root/app.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var React = require('react');
33
var Router = require('../../modules/main');
44
var Route = Router.Route;
5+
var Routes = Router.Routes;
56
var Link = Router.Link;
67

78
var App = React.createClass({
@@ -66,15 +67,17 @@ var ForgotPassword = React.createClass({
6667
});
6768

6869
var routes = (
69-
<Route handler={App}>
70-
<Route handler={SignedOut}>
71-
<Route name="signin" handler={SignIn}/>
72-
<Route name="forgot-password" handler={ForgotPassword}/>
70+
<Routes>
71+
<Route handler={App}>
72+
<Route handler={SignedOut}>
73+
<Route name="signin" handler={SignIn}/>
74+
<Route name="forgot-password" handler={ForgotPassword}/>
75+
</Route>
76+
<Route handler={SignedIn}>
77+
<Route name="home" handler={Home}/>
78+
</Route>
7379
</Route>
74-
<Route handler={SignedIn}>
75-
<Route name="home" handler={Home}/>
76-
</Route>
77-
</Route>
80+
</Routes>
7881
);
7982

8083
React.renderComponent(routes, document.body);

examples/shared-root/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<!doctype html public "superawesome">
22
<title>Shared Root Example</title>
3+
<link rel="stylesheet" href="../app.css"/>
34
<body>
45
<script src="../build/shared-root.js"></script>

examples/simple-master-detail/app.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var React = require('react');
33
var Router = require('../../modules/main');
44
var Route = Router.Route;
5+
var Routes = Router.Routes;
56
var Link = Router.Link;
67

78
var App = React.createClass({
@@ -50,9 +51,11 @@ var State = React.createClass({
5051
});
5152

5253
var routes = (
53-
<Route handler={App}>
54-
<Route name="state" path="state/:abbr" handler={State}/>
55-
</Route>
54+
<Routes>
55+
<Route handler={App}>
56+
<Route name="state" path="state/:abbr" handler={State}/>
57+
</Route>
58+
</Routes>
5659
);
5760

5861
React.renderComponent(routes, document.body);

examples/transitions/app.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var React = require('react');
33
var Router = require('../../modules/main');
44
var Route = Router.Route;
5+
var Routes = Router.Routes;
56
var Link = Router.Link;
67

78
var App = React.createClass({
@@ -55,10 +56,12 @@ var Form = React.createClass({
5556
});
5657

5758
var routes = (
58-
<Route handler={App}>
59-
<Route name="dashboard" handler={Dashboard}/>
60-
<Route name="form" handler={Form}/>
61-
</Route>
59+
<Routes>
60+
<Route handler={App}>
61+
<Route name="dashboard" handler={Dashboard}/>
62+
<Route name="form" handler={Form}/>
63+
</Route>
64+
</Routes>
6265
);
6366

6467
React.renderComponent(routes, document.body);

0 commit comments

Comments
 (0)