Skip to content

Commit c739fe5

Browse files
committed
updated the overview
1 parent e1a18bc commit c739fe5

File tree

1 file changed

+122
-69
lines changed

1 file changed

+122
-69
lines changed

docs/guides/overview.md

Lines changed: 122 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Let's imagine a little app with a dashboard, inbox, and calendar.
1010
```
1111
+---------------------------------------------------------+
1212
| +---------+ +-------+ +--------+ |
13-
| |Dashboard| | Inbox | |Calendar| Logged in as Joe |
13+
| |Dashboard| | Inbox | |Calendar| Logged in as Jane |
1414
| +---------+ +-------+ +--------+ |
1515
+---------------------------------------------------------+
1616
| |
@@ -48,7 +48,7 @@ var Header = React.createClass({
4848
<li><a href="/inbox">Inbox</a></li>
4949
<li><a href="/calendar">Calendar</a></li>
5050
</ul>
51-
Logged in as Joe
51+
Logged in as Jane
5252
</header>
5353
);
5454
}
@@ -99,30 +99,49 @@ otherRouter.route('/inbox', function () {
9999
otherRouter.route('/calendar', function () {
100100
React.renderComponent(<CalendarRoute/>, document.body);
101101
});
102-
103102
```
104103

105104
The three main view's render methods are nearly identical. While one
106105
level of shared UI like this is pretty easy to handle, getting deeper
107-
and deeper adds more complexity, along with lots of `switch` branching,
108-
etc.
106+
and deeper adds more complexity.
109107

110-
React Router embraces this common pattern among user interfaces by
111-
nesting the views for you.
108+
Other approaches might introduce a lot of `if/else` or `switch/case`
109+
into your app if you pass the route information down through the app
110+
hierarchy via props.
111+
112+
```js
113+
var App = React.createClass({
114+
render: function () {
115+
var page;
116+
switch (this.props.page) {
117+
case 'dashboard': page = <Dashboard/>; break;
118+
case 'inbox': page = <Dashboard/>; break;
119+
default: page = <Index/>; break;
120+
}
121+
return (
122+
<div>{page}</div>
123+
);
124+
}
125+
});
126+
```
127+
128+
React Router embraces the common pattern of shared UI among user
129+
interfaces by nesting the views for you, decreasing boilerplate.
112130

113131
With React Router
114132
-----------------
115133

116134
Here's how it works:
117135

118136
1. You declare your view hierarchy with nested `<Route/>`s and provide
119-
them with a React component to handle the route when its active.
137+
them with a React element to handle the route when its active.
120138

121139
2. React Router will match the deepest route against the URL, and then
122140
activate the entire tree of routes on that branch, nesting all the
123141
UI.
124142

125-
3. You access the active route handler in the props of the parent route.
143+
3. You simply call `<RouteHandler/>` and it will render the active child
144+
route.
126145

127146
```js
128147
var App = React.createClass({
@@ -135,41 +154,46 @@ var App = React.createClass({
135154
<li><Link to="inbox">Inbox</Link></li>
136155
<li><Link to="calendar">Calendar</Link></li>
137156
</ul>
138-
Logged in as Joe
157+
Logged in as Jane
139158
</header>
140159

141160
{/* this is the important part */}
142-
<this.props.activeRouteHandler/>
161+
<RouteHandler/>
143162
</div>
144163
);
145164
}
146165
});
147166

148167
var routes = (
149-
<Routes location="history">
150-
<Route name="app" path="/" handler={App}>
151-
<Route name="inbox" handler={Inbox}/>
152-
<Route name="calendar" handler={Calendar}/>
153-
<DefaultRoute handler={Dashboard}/>
154-
</Route>
155-
</Routes>
168+
<Route name="app" path="/" handler={App}>
169+
<Route name="inbox" handler={Inbox}/>
170+
<Route name="calendar" handler={Calendar}/>
171+
<DefaultRoute handler={Dashboard}/>
172+
</Route>
156173
);
157174

158-
React.renderComponent(routes, document.body);
175+
Router.run(routes, function(Handler) {
176+
React.renderComponent(<Handler/>, document.body);
177+
});
178+
159179
```
160180

161181
When the user lands at `/inbox`, the route named `inbox` gets matched so
162-
its parent route will render the `App` component, and since `inbox` is
163-
active, you get `Inbox` as `this.props.activeRouteHandler`. This is
164-
nearly identical to `{{outlet}}` from Ember or `<div ng-view/>` from
165-
angular.
182+
its parent route, `app`, is also matched. The `run` callback receives
183+
`Handler`, that has all of this match information wrapped up in it.
184+
185+
Rendering `Handler` is really just rendering `App` since its the highest
186+
matched route handler. Since `inbox` is the active child route,
187+
rendering `<RouteHandler/>` in `App` renders the `Inbox` component.
188+
`<RouteHandler/>` is nearly identical to `{{outlet}}` from Ember or
189+
`<div ng-view/>` from angular.
166190

167191
When the user navigates to `/calendar`, the same thing happens except
168-
now `Calendar` is the `activeRouteHandler` in `App`'s render method.
192+
now `Calendar` is the `<RouteHandler/>` in `App`'s render method.
169193

170194
Finally, when the user navigates to the path `/`, `App` is active, and
171-
notices that it has a `DefaultRoute`, so it receives `Dashboard` as the
172-
`activeRouteHandler`. If a `DefaultRoute` is defined, it will be active
195+
notices that it has a `DefaultRoute`, so `Dashboard` becomes the new
196+
`<RouteHandler/>`. If a `DefaultRoute` is defined, it will be active
173197
when the parent's route is matched exactly.
174198

175199
Note that we don't need the `<Header/>` component since we don't have to
@@ -187,7 +211,7 @@ navigates through the messages.
187211
```
188212
+---------------------------------------------------------------------+
189213
| +---------+ +-------+ +--------+ |
190-
| |Dashboard| | Inbox | |Calendar| Logged in as Joe |
214+
| |Dashboard| | Inbox | |Calendar| Logged in as Jane |
191215
| +---------+ +-------+ +--------+ |
192216
+---------------------------------------------------------------------+
193217
| +---------+ +-------+ +--------------+ |
@@ -220,43 +244,42 @@ var Inbox = React.createClass({
220244
<div>
221245
<Toolbar/>
222246
<Messages/>
223-
<this.props.activeRouteHandler/>
247+
<RouteHandler/>
224248
</div>
225249
);
226250
}
227251
});
228252

229253
var routes = (
230-
<Routes location="history">
231-
<Route handler={App}>
254+
<Route handler={App}>
232255

233-
<Route name="inbox" handler={Inbox}>
234-
<Route name="message" path=":messageId" handler={Message}/>
235-
<DefaultRoute handler={InboxStats}/>
236-
</Route>
256+
<Route name="inbox" handler={Inbox}>
257+
<Route name="message" path=":messageId" handler={Message}/>
258+
<DefaultRoute handler={InboxStats}/>
259+
</Route>
237260

238-
<Route name="calendar" handler={Calendar}/>
239-
<DefaultRoute handler={Dashboard}/>
261+
<Route name="calendar" handler={Calendar}/>
262+
<DefaultRoute handler={Dashboard}/>
240263

241-
</Route>
242-
</Routes>
264+
</Route>
243265
);
244266
```
245267

246-
- Inbox now has `this.props.activeRouteHandler` in its render method,
268+
- Inbox now has a `<RouteHandler/>` in its render method,
247269
exactly like its parent.
248-
- We added a child routes to `inbox`; messages or the stats page can now
270+
- We added child routes to `inbox`; the messages or stats page can now
249271
render into it.
250272

251273
Nesting a new level of UI does not increase the complexity of your code.
252-
You simply nest some routes and render them with `activeRouteHandler`.
274+
You simply nest some routes and render them with `<RouteHandler/>`.
253275

254276
Dynamic Segments
255277
----------------
256278

257279
When we added the `message` route, we introduced a "dynamic segment" to
258-
the URL. These segements get parsed from the url and passed into your
259-
route handler on `this.props.params`.
280+
the URL. These segements get parsed from the url and are available in
281+
the `run` callback, or from the `State` mixin. Let's see how we can
282+
access the params.
260283

261284
Remember our message route looks like this:
262285

@@ -268,17 +291,54 @@ Lets look at accessing the `messageId` in `Message`.
268291

269292
```js
270293
var Message = React.createClass({
294+
mixins: [Router.State],
271295
render: function () {
272296
return (
273-
<div>{this.props.params.messageId}</div>
297+
<div>{this.getParams().messageId}</div>
274298
);
275299
}
276300
});
277301
```
278302

279-
Assuming the user navigates to `/inbox/123`, `this.props.params.messageId` is
303+
Assuming the user navigates to `/inbox/123`, `this.getParams().messageId` is
280304
going to be `'123'`.
281305

306+
Alternatively, you can pass the param data down through the view
307+
hierarchy from the `run` callback and access the params with
308+
`this.props.params`.
309+
310+
```js
311+
Router.run(routes, function(Handler, state) {
312+
var params = state.params;
313+
React.renderComponent(<Handler params={params}/>, document.body);
314+
});
315+
316+
// and then pass the params down to every use of `<RouteHandler/>`
317+
<RouteHandler {...this.props}/>
318+
319+
// Inbox ends up looking like this
320+
var Inbox = React.createClass({
321+
render: function () {
322+
return (
323+
<div>
324+
<Toolbar/>
325+
<Messages/>
326+
<RouteHandler {...this.props}/>
327+
</div>
328+
);
329+
}
330+
});
331+
332+
// and Message changes to:
333+
var Message = React.createClass({
334+
render: function () {
335+
return (
336+
<div>{this.props.params.messageId}</div>
337+
);
338+
}
339+
});
340+
```
341+
282342
Important Note About Dynamic Segments
283343
-------------------------------------
284344

@@ -291,10 +351,6 @@ component whose props are changing. This way you can leverage the
291351
performance of the React DOM diff algorithm. Look at the `Contact`
292352
handler in the `master-detail` example.
293353

294-
If you'd rather be lazy, you can use the `addHandlerKey` option and set
295-
it to `true` on your route to opt-out of the performance. See also
296-
[Route][Route].
297-
298354
Scrolling
299355
---------
300356

@@ -321,23 +377,21 @@ At any level of your UI nesting, you can render a handler if the url
321377
beyond what was matched isn't recognized.
322378

323379
```xml
324-
<Routes location="history">
325-
<Route path="/" handler={App}>
326-
<Route name="inbox" path="/inbox" handler={Inbox}>
327-
<!--
328-
will render inside the `Inbox` UI for any paths not recognized
329-
after the parent route's path `/inbox/*`
330-
-->
331-
<NotFoundRoute handler={InboxNotFound}/>
332-
<Route name="message" path="/inbox/:messageId" handler={Message}/>
333-
<DefaultRoute handler={InboxStats}/>
334-
</Route>
335-
<Route name="calendar" path="/calendar" handler={Calendar}/>
336-
<DefaultRoute handler={Dashboard}/>
380+
<Route path="/" handler={App}>
381+
<Route name="inbox" path="/inbox" handler={Inbox}>
382+
<!--
383+
will render inside the `Inbox` UI for any paths not recognized
384+
after the parent route's path `/inbox/*`
385+
-->
386+
<NotFoundRoute handler={InboxNotFound}/>
387+
<Route name="message" path="/inbox/:messageId" handler={Message}/>
388+
<DefaultRoute handler={InboxStats}/>
337389
</Route>
338-
<!-- will catch any route that isn't recognized at all -->
339-
<NotFoundRoute handler={NotFound}/>
340-
</Routes>
390+
<Route name="calendar" path="/calendar" handler={Calendar}/>
391+
<DefaultRoute handler={Dashboard}/>
392+
</Route>
393+
<!-- will catch any route that isn't recognized at all -->
394+
<NotFoundRoute handler={NotFound}/>
341395
```
342396

343397
### `<Redirect/>`
@@ -346,7 +400,7 @@ URLs in an app change, so we made it easy to not break the old ones.
346400

347401
```xml
348402
<Route name="message" path="/inbox/:messageId" handler={Message} />
349-
<Redirect path="/messages/:messageId" to="message" />
403+
<Redirect from="/messages/:messageId" to="message" />
350404
```
351405

352406
Path Matching
@@ -363,8 +417,8 @@ it has to offer. Check out the [API Docs][API] to learn about
363417
redirecting transitions, query parameters and more.
364418

365419
[AsyncState]:../api/mixins/AsyncState.md
366-
[Route]:../api/elements/Route.md
367-
[Routes]:../api/elements/Routes.md
420+
[Route]:../api/components/Route.md
421+
[Routes]:../api/components/Routes.md
368422
[API]:../api/
369423
[path-matching]:./path-matching.md
370424

@@ -376,7 +430,6 @@ In order for the above examples to work in a CommonJS environment you'll need to
376430
```
377431
var Router = require('react-router');
378432
var Route = Router.Route;
379-
var Routes = Router.Routes;
380433
var NotFoundRoute = Router.NotFoundRoute;
381434
var DefaultRoute = Router.DefaultRoute;
382435
var Link = Router.Link;

0 commit comments

Comments
 (0)