Skip to content

Commit f0ea1cb

Browse files
committed
docs update for new api changes
1 parent 2c44032 commit f0ea1cb

File tree

10 files changed

+266
-36
lines changed

10 files changed

+266
-36
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,23 @@ What's it look like?
7070
```js
7171
React.renderComponent((
7272
<Routes location="history">
73-
<Route handler={App}>
74-
<Route name="about" handler={About}/>
73+
<Route path="/" handler={App}>
74+
<DefaultRoute handler={Home} />
75+
<Route name="about" handler={About} />
7576
<Route name="users" handler={Users}>
76-
<Route name="user" path="/user/:userId" handler={User}/>
77+
<Route name="recent-users" path="recent" handler={User} />
78+
<Route name="user" path="/user/:userId" handler={User} />
79+
<NotFoundRoute handler={UserRouteNotFound}/>
7780
</Route>
7881
</Route>
79-
<Route path="*" handler={NotFound}/>
82+
<NotFoundRoute handler={NotFound}/>
83+
<Redirect path="company" to="about" />
8084
</Routes>
8185
), document.body);
8286
```
8387

88+
All of the `handler`s will render inside their parent route `handler`.
89+
8490
See more in the [overview guide](/docs/guides/overview.md).
8591

8692
Benefits of This Approach

UPGRADE_GUIDE.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,59 @@ they refer to.
88
0.5.x -> master
99
---------------
1010

11+
### Path Matching
12+
13+
Paths that start with `/` are absolute and work exactly as they used to.
14+
Paths that don't start with `/` are now relative, meaning they extend
15+
their parent route.
16+
17+
Simply add `/` in front of all your paths to keep things working.
18+
19+
```xml
20+
<!-- 0.5.x -->
21+
<Route path="/foo">
22+
<Route path="bar"/>
23+
</Route>
24+
25+
<!-- 0.6.x -->
26+
<Route path="/foo">
27+
<Route path="/bar"/>
28+
</Route>
29+
```
30+
31+
Though, you may want to embrace this new feature:
32+
33+
```js
34+
<!-- 0.5.x -->
35+
<Route path="/course/:courseId">
36+
<Route path="/course/:courseId/assignments"/>
37+
<Route path="/course/:courseId/announcements"/>
38+
</Route>
39+
40+
<!-- 0.6.x -->
41+
<Route path="/course/:courseId">
42+
<Route path="assignments"/>
43+
<Route path="announcements"/>
44+
</Route>
45+
```
46+
47+
Also `.` is no longer matched in dynamic segments.
48+
49+
```js
50+
<!-- 0.5.x -->
51+
<Route path="/file/:filename" />
52+
53+
<!-- 0.6.x -->
54+
<Route path="/file/:filename.:ext?" />
55+
56+
<!--
57+
or for a looser match to allow for multiple `.` note that the data
58+
will be available on `this.props.params.splat` instead of
59+
`this.props.params.filename`
60+
-->
61+
<Route path="/file/*" />
62+
```
63+
1164
### Link params
1265

1366
Links should now pass their params in the `params` property, though the

docs/api/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ React Router API
66
- Components
77
- [`DefaultRoute`](/docs/api/components/DefaultRoute.md)
88
- [`Link`](/docs/api/components/Link.md)
9+
- [`NotFoundRoute`](/docs/api/components/NotFoundRoute.md)
910
- [`Redirect`](/docs/api/components/Redirect.md)
1011
- [`Route`](/docs/api/components/Route.md)
1112
- [`RouteHandler`](/docs/api/components/RouteHandler.md)

docs/api/components/DefaultRoute.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,7 @@ route path is matched exactly.
1010
Props
1111
-----
1212

13-
### `handler`
14-
15-
The component to be rendered when the route is active.
16-
17-
### `preserveScrollPosition`
18-
19-
If `true`, the router will not scroll the window up when the route is
20-
transitioned to. Defaults to `false`. Ignored if the parent `<Routes/>`
21-
has been set to `true`.
13+
See [Route::props][routeProps]
2214

2315
Example
2416
-------
@@ -60,3 +52,4 @@ same functionality:
6052
`DefaultRoute` feels more natural, so you can name and transition to the
6153
parent route.
6254

55+
[routeProps]:/docs/api/components/Route.md#props
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
API: `NotFoundRoute` (component)
2+
===============================
3+
4+
When a parent's url partially matches, but none of the children do, a
5+
`NotFoundRoute` will be matched and its handler rendered at any level of
6+
your route/view hierarchy.
7+
8+
Props
9+
-----
10+
11+
See [Route::props][routeProps]
12+
13+
Example
14+
-------
15+
16+
```xml
17+
<Routes>
18+
<Route path="/" handler={App}>
19+
<Route name="course" path="course/:courseId" handler={Course}>
20+
<Route name="course-dashboard" path="dashboard" />
21+
22+
<!-- ie: `/course/123/foo` -->
23+
<NotFoundRoute handler={CourseRouteNotFound} />
24+
</Route>
25+
26+
<!-- ie: `/flkjasdf` -->
27+
<NotFoundRoute handler={NotFound} />
28+
</Route>
29+
</Routes>
30+
```
31+
32+
The last `NotFoundRoute` will render inside the `App`, the first
33+
34+
[routeProps]:/docs/api/components/Route.md#props
35+

docs/api/components/Redirect.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Props
88

99
### `from`
1010

11-
The path you want to redirect from, including dynamic segments.
11+
The path you want to redirect from, including dynamic segments. Defaults
12+
to `*` so you can redirect anything not found to somewhere else.
1213

1314
### `to`
1415

@@ -26,9 +27,20 @@ Example
2627
<Route handler={App}>
2728
<Route name="contact" handler={Contact}/>
2829
<Route name="about-user" path="about/:userId" handler={UserProfile}/>
30+
<Route name="course" path="course/:courseId">
31+
<Route name="course-dashboard" path="dashboard" handler={Dashboard}/>
32+
<Route name="course-assignments" path="assignments" handler={Assignments}/>
33+
<!--
34+
anything like `/course/123/invalid` redirects to
35+
`/course/123/dashboard`
36+
-->
37+
<Redirect to="course-dashboard" />
38+
</Route>
2939
</Route>
3040

41+
<!-- `/get-in-touch` -> `/contact` -->
3142
<Redirect from="get-in-touch" to="contact" />
43+
<!-- `/profile/123` -> `/about/123` -->
3244
<Redirect from="profile/:userId" to="about-user" />
3345
</Routes>
3446
```

docs/api/components/Route.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ transition methods.
1313

1414
### `path`
1515

16-
The path used in the URL, supporting dynamic segments. If left
17-
undefined, the path will be defined by the `name`, and if there is no
18-
name, will default to `/`. This path is always absolute from the URL
19-
"root", even if the leading slash is left off. Nested routes do not
20-
inherit the path of their parent.
16+
The path used in the URL. If left undefined, the path will be defined by
17+
the `name`, and if there is no name, will default to `/`.
18+
19+
Please refer to the [Path Matching Guide][path-matching] to learn more
20+
about supported path matching syntax.
2121

2222
### `handler`
2323

@@ -102,3 +102,4 @@ Example
102102
</Routes>
103103
```
104104

105+
[path-matching]:/docs/guides/path-matching.md

docs/api/components/RouteHandler.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Static Lifecycle Methods
6161
You can define static methods on your route handlers that will be called
6262
during route transitions.
6363

64-
### `willTransitionTo(transition, params)`
64+
### `willTransitionTo(transition, params, query)`
6565

6666
Called when a route is about to render, giving you the opportunity to
6767
abort or redirect the transition. You can return a promise and the whole

docs/guides/overview.md

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ var App = React.createClass({
148148
var routes = (
149149
<Routes location="history">
150150
<Route name="app" path="/" handler={App}>
151-
<Route name="inbox" path="/inbox" handler={Inbox}/>
152-
<Route name="calendar" path="/calendar" handler={Calendar}/>
151+
<Route name="inbox" handler={Inbox}/>
152+
<Route name="calendar" handler={Calendar}/>
153153
<DefaultRoute handler={Dashboard}/>
154154
</Route>
155155
</Routes>
@@ -228,14 +228,14 @@ var Inbox = React.createClass({
228228

229229
var routes = (
230230
<Routes location="history">
231-
<Route path="/" handler={App}>
231+
<Route handler={App}>
232232

233-
<Route name="inbox" path="/inbox" handler={Inbox}>
234-
<Route name="message" path="/inbox/:messageId" handler={Message}/>
233+
<Route name="inbox" handler={Inbox}>
234+
<Route name="message" path=":messageId" handler={Message}/>
235235
<DefaultRoute handler={InboxStats}/>
236236
</Route>
237237

238-
<Route name="calendar" path="/calendar" handler={Calendar}/>
238+
<Route name="calendar" handler={Calendar}/>
239239
<DefaultRoute handler={Dashboard}/>
240240

241241
</Route>
@@ -251,13 +251,6 @@ var routes = (
251251
Nesting a new level of UI does not increase the complexity of your code.
252252
You simply nest some routes and render them with `activeRouteHandler`.
253253

254-
**Note**: the paths are not inherited from parent to child. This gives
255-
you the flexibility to have any url you need. Were the paths to be
256-
inherited, you'd be forced to couple your urls to your route hierarchy.
257-
For example, we may want `/inbox` and `/messages/123` instead of `/inbox`
258-
and `/inbox/123`. We can just change the path on the `message` route and
259-
still get view nesting even though the urls are not nested.
260-
261254
Dynamic Segments
262255
----------------
263256

@@ -268,7 +261,7 @@ route handler on `this.props.params`.
268261
Remember our message route looks like this:
269262

270263
```xml
271-
<Route name="message" path="/inbox/:messageId" handler={Message}/>
264+
<Route name="message" path=":messageId" handler={Message}/>
272265
```
273266

274267
Lets look at accessing the `messageId` in `Message`.
@@ -305,15 +298,66 @@ If you'd rather be lazy, you can use the `addHandlerKey` option and set
305298
it to `true` on your route to opt-out of the performance. See also
306299
[Route][Route].
307300

308-
Links
309-
-----
301+
Scrolling
302+
---------
303+
304+
By default, the router will manage the scroll position between route
305+
transitions. When a user clicks "back" or "forward", it will restore
306+
their scroll position. If they visit a new route, it will automatically
307+
scroll the window to the top. You can opt out of this with the
308+
`preserverScrollPosition` option on [Routes][Routes] or [Route][Route].
309+
310+
Bells and Whistles
311+
------------------
312+
313+
### `<Link/>`
310314

311315
The `<Link/>` component allows you to conveniently navigate users around
312316
the application with accessible anchor tags that don't break normal link
313317
functionality like control/command clicking to open in a new tab. Also,
314318
when the route a link references is active, you get the `active` css
315319
class to easily style your UI.
316320

321+
### `<NotFoundRoute/>`
322+
323+
At any level of your UI nesting, you can render a handler if the url
324+
beyond what was matched isn't recognized.
325+
326+
```xml
327+
<Routes location="history">
328+
<Route path="/" handler={App}>
329+
<Route name="inbox" path="/inbox" handler={Inbox}>
330+
<!--
331+
will render inside the `Inbox` UI for any paths not recognized
332+
after the parent route's path `/inbox/*`
333+
-->
334+
<NotFoundRoute handler={InboxNotFound}
335+
<Route name="message" path="/inbox/:messageId" handler={Message}/>
336+
<DefaultRoute handler={InboxStats}/>
337+
</Route>
338+
<Route name="calendar" path="/calendar" handler={Calendar}/>
339+
<DefaultRoute handler={Dashboard}/>
340+
</Route>
341+
<!-- will catch any route that isn't recognized at all -->
342+
<NotFoundRoute handler={NotFound}
343+
</Routes>
344+
```
345+
346+
### `<Redirect/>`
347+
348+
URLs in an app change, so we made it easy to not break the old ones.
349+
350+
```xml
351+
<Route name="message" path="/inbox/:messageId" handler={Message} />
352+
<Redirect path="/messages/:messageId" to="message" />
353+
```
354+
355+
Path Matching
356+
-------------
357+
358+
There's a lot more to be said about path matching, check out the [Path
359+
Matching Guide][path-matching].
360+
317361
API Documentation
318362
-----------------
319363

@@ -323,5 +367,7 @@ redirecting transitions, query parameters and more.
323367

324368
[AsyncState]:../api/mixins/AsyncState.md
325369
[Route]:../api/components/Route.md
370+
[Routes]:../api/components/Routes.md
326371
[API]:../api/
372+
[path-matching]:./path-matching.md
327373

0 commit comments

Comments
 (0)