Skip to content

Commit 81a8edf

Browse files
committed
holy smokes new website
1 parent a141ab5 commit 81a8edf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2144
-471
lines changed

doc/00 Guides/Router Overview.md

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
To illustrate the problems React Router is going to solve for you, let’s build a
2+
small application without it.
3+
4+
Without React Router
5+
--------------------
6+
7+
```js
8+
var About = React.createClass({
9+
render: function () {
10+
return <h2>About</h2>;
11+
}
12+
});
13+
14+
var Inbox = React.createClass({
15+
render: function () {
16+
return <h2>Inbox</h2>;
17+
}
18+
});
19+
20+
var Home = React.createClass({
21+
render: function () {
22+
return <h2>Home</h2>;
23+
}
24+
});
25+
26+
var App = React.createClass({
27+
render () {
28+
var Child;
29+
switch (this.props.route) {
30+
case 'about': Child = About; break;
31+
case 'inbox': Child = Inbox; break;
32+
default: Child = Home;
33+
}
34+
35+
return (
36+
<div>
37+
<h1>App</h1>
38+
<Child/>
39+
</div>
40+
)
41+
}
42+
});
43+
44+
function render () {
45+
var route = window.location.hash.substr(1);
46+
React.render(<App route={route} />, document.body);
47+
}
48+
49+
window.addEventListener('hashchange', render);
50+
render(); // render initially
51+
```
52+
53+
As the hash portion of the URL changes, `App` will render a different
54+
`<Child/>` by branching on `this.props.route`. Pretty straightforward
55+
stuff. But it gets complicated fast.
56+
57+
Imagine now that `Inbox` has some nested UI at a path like
58+
`inbox/messages/:id` and `inbox/unread`, etc. We'll need to make our url
59+
parsing much more intelligent to be able to pass the right information
60+
to `App`, and then to `Inbox` in order for it to know which URL-mapped
61+
child component it should render. We'd then have a branch of components
62+
that should be rendered at any given URL. Add a few more of these
63+
branches and we'll end up with a lot of code to keep the URL and our
64+
application's component hierarchy in sync.
65+
66+
With React Router
67+
-----------------
68+
69+
Nested URLs and nested component hierarchy are at the heart of React
70+
Router. Lets make our routing for our little app declarative. We use JSX
71+
for route configuration because we want to define a view hierarchy with
72+
properties, so its a pefect fit.
73+
74+
```js
75+
var Router = require('react-router');
76+
var Route = Router.Route;
77+
78+
// declare our routes and their hierarchy
79+
var routes = (
80+
<Route handler={App}>
81+
<Route path="about" handler={About}/>
82+
<Route path="inbox" handler={Inbox}/>
83+
</Route>
84+
);
85+
```
86+
87+
Next we need to delete some code from `App`. We'll replace `<Child/>`
88+
with `<RouteHandler/>` that functions as the old `switch` block from
89+
before.
90+
91+
```js
92+
var RouteHandler = Router.RouteHandler;
93+
94+
var App = React.createClass({
95+
render () {
96+
return (
97+
<div>
98+
<h1>App</h1>
99+
<RouteHandler/>
100+
</div>
101+
)
102+
}
103+
});
104+
```
105+
106+
Finally we need to listen to the url and render the application.
107+
108+
```js
109+
Router.run(routes, Router.HashLocation, (Root) => {
110+
React.render(<Root/>, document.body);
111+
});
112+
```
113+
114+
`Root` is a component that bakes in the matched component hierarchy
115+
making `RouteHandler` know what to render.
116+
117+
Note that `<Route/>` components are not ever rendered, they are just
118+
configuration objects that the router uses to create an internal tree of
119+
routes.
120+
121+
Adding more UI
122+
--------------
123+
124+
Alright, now we're ready to nest the inbox messages inside the inbox UI.
125+
First we'll make a new `Message` component and then we'll add the route
126+
under `inbox` so that the UI will nest.
127+
128+
```js
129+
var Message = React.createClass({
130+
render () {
131+
return <h3>Message</h3>;
132+
}
133+
});
134+
135+
var routes = (
136+
<Route handler={App}>
137+
<Route path="about" handler={About}/>
138+
<Route path="inbox" handler={Inbox}>
139+
<Route path="messages/:id" handler={Message}/>
140+
</Route>
141+
</Route>
142+
);
143+
```
144+
145+
Now visits to urls like `inbox/messages/Jkei3c32` will match the new
146+
route and nest the UI branch of `App -> Inbox -> Message`.
147+
148+
Getting the url parameters
149+
--------------------------
150+
151+
We're going to need to know something about the message in order to
152+
fetch it from the server. We call the component you hand to a `<Route/>`
153+
a `RouteHandler`. `RouteHandler` instances get some useful properties
154+
injected into them when you render, particularly the parameters from the
155+
dynamic segment of your path. In our case, `:id`.
156+
157+
```js
158+
var Message = React.createClass({
159+
componentDidMount: function () {
160+
// from the path `/inbox/messages/:id`
161+
var id = this.props.params.id;
162+
fetchMessage(id, function (err, message) {
163+
this.setState({ message: message });
164+
})
165+
},
166+
// ...
167+
});
168+
```
169+
170+
Nested UI and Nested URLs need not be coupled
171+
---------------------------------------------
172+
173+
With React Router, you don't need to nest your UI in order to get a
174+
nested URL. Inversely, to get nested UI, you don't need to have nested
175+
URLs either.
176+
177+
Lets make a new url at `/about/company`, but without nesting the UI
178+
inside of the `About` component.
179+
180+
```js
181+
var routes = (
182+
<Route handler={App}>
183+
<Route path="about" handler={About}/>
184+
<Route path="about/company" handler={Company}/>
185+
</Route>
186+
);
187+
```
188+
189+
Though our url is nested, the UI of `About` and `Company` are siblings.
190+
191+
Now lets go the other way and add the url `/archive/messages/:id` and
192+
have it nested under our inbox UI even though the URL is not nested. We
193+
have to do three things for this to work:
194+
195+
1. Start the path with `/` to signal that its an absolute path. This
196+
won’t “inherit” the parent path the way `inbox/messages/:id` gets
197+
inherited.
198+
2. Nest the `<Route/>` under the `inbox` route to cause the UI to nest.
199+
3. Ensure you have all the necessary dynamic segments, we only have
200+
`:id` so its pretty easy.
201+
202+
```js
203+
var routes = (
204+
<Route handler={App}>
205+
<Route path="inbox" handler={Inbox}>
206+
<Route path="messages/:id" handler={Message}/>
207+
<Route path="/archive/messages/:id" handler={Message}/>
208+
</Route>
209+
</Route>
210+
);
211+
```
212+
213+
That's the gist of React Router. Application UIs are boxes inside of
214+
boxes inside of boxes; now you can keep those boxes in sync with the
215+
URL.
216+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
A `DefaultRoute` will be the matched child route when the parent's path
2+
matches exactly.
3+
4+
You'd want to use this to ensures a child `RouteHandler` is always
5+
rendered when there is no child match. Think of it like `index.html` in
6+
a directory of a static html server.
7+
8+
Props
9+
-----
10+
11+
### `handler`
12+
13+
The `RouteHandler` component you want to be rendered when the route is
14+
matched.
15+
16+
### `name` (optional)
17+
18+
The name of the route used when linking or transitioning to the route.
19+
20+
Example
21+
-------
22+
23+
```xml
24+
<Route path="/" handler={App}>
25+
26+
<!--
27+
When the url is `/`, this route will be active. In other
28+
words, `Home` will be the `<RouteHandler/>` in `App`.
29+
-->
30+
<DefaultRoute handler={Home}/>
31+
32+
<Route name="about" handler={About}/>
33+
<Route name="users" handler={Users}>
34+
<Route name="user" handler={User} path="/user/:id"/>
35+
36+
<!-- when the url is `/users`, this will be active -->
37+
<DefaultRoute name="users-index" handler={UsersIndex}/>
38+
39+
</Route>
40+
</Route>
41+
```
42+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
A `NotFoundRoute` is active when the beginning of its parent's path
2+
matches the URL but none of its siblings do. It can be found at any level
3+
of your hierarchy, allowing you to have a context-aware "not found"
4+
screens.
5+
6+
You'd want to use this to handle bad links and users typing invalid urls
7+
into the address bar.
8+
9+
**Note**: This is not intended to be used for when a _resource_ is not
10+
found. There is a difference between the router not finding a matched
11+
path and a valid URL that results in a resource not being found. The
12+
url `courses/123` is a valid url and results in a matched route,
13+
therefore it was "found" as far as routing is concerned. Then, if we
14+
fetch some data and discover that the course `123` does not exist, we do
15+
not want to transition to a new route. Just like on the server, you go
16+
ahead and serve the url but render different UI (and use a different
17+
status code). You shouldn't ever try to transition to a `NotFoundRoute`.
18+
19+
Props
20+
-----
21+
22+
### `handler`
23+
24+
The `RouteHandler` component you want to be rendered when the route is
25+
matched.
26+
27+
Example
28+
-------
29+
30+
```xml
31+
<Route path="/" handler={App}>
32+
<Route name="course" path="course/:courseId" handler={Course}>
33+
<Route name="course-dashboard" path="dashboard" handler={Dashboard}/>
34+
35+
<!-- ie: `/course/123/foo` -->
36+
<NotFoundRoute handler={CourseRouteNotFound} />
37+
</Route>
38+
39+
<!-- ie: `/flkjasdf` -->
40+
<NotFoundRoute handler={NotFound} />
41+
</Route>
42+
```
43+
44+
The last `NotFoundRoute` will render inside the `App`, the first will
45+
rendering inside of `Course`.
46+

docs/api/components/Redirect.md renamed to doc/01 Route Configuration/Redirect.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
API: `Redirect` (component)
2-
===========================
3-
4-
A `<Redirect>` sets up a redirect to another route in your application.
1+
A `Redirect` sets up a redirect to another route in your application to
2+
maintain old URLs.
53

64
Props
75
-----

docs/api/components/Route.md renamed to doc/01 Route Configuration/Route.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
API: `Route` (component)
2-
=========================
3-
4-
A `<Route>` is used to declare your application's routes and entry view hierarchy.
1+
A `Route` is used to declaratively map routes to your application's
2+
screen hiearchy.
53

64
Props
75
-----
86

9-
### `name`
7+
### `name` (optional)
108

11-
The unique name of the route, used in the `Link` component and the router's transition methods.
9+
The unique name of the route, used in the `Link` component and the
10+
router's transition methods.
1211

13-
### `path`
12+
### `path` (optional)
1413

1514
The path used in the URL. If left undefined, the path will be defined by
1615
the `name`, and if there is no name, will default to `/`.
@@ -20,7 +19,7 @@ about supported path matching syntax.
2019

2120
### `handler`
2221

23-
The component to be rendered when the route is active.
22+
The `RouteHandler` component to be rendered when the route is active.
2423

2524
### `children`
2625

@@ -30,11 +29,17 @@ is a very critical part of the router's design.
3029

3130
### `ignoreScrollBehavior`
3231

33-
When route or its `params` change, router adjusts window scroll position according to [`scrollBehavior`](https://github.com/rackt/react-router/blob/master/docs/api/create.md#scrollbehavior). This is generally desirable but you might want to opt out of scrolling adjustment for a specific route or a group of routes.
32+
When a route or its `params` change, the router adjusts window scroll
33+
position according to the [`scrollBehavior`][scrollbehavior]. This is
34+
generally desirable but you might want to opt-out of scrolling
35+
adjustment for a specific route or a group of routes.
3436

35-
If you specify `ignoreScrollBehavior` attribute on a route, changes in `params` or any transitions within its `children` will not adjust scroll position. This can be useful on a search page or in a tabbed interface.
37+
If you specify `ignoreScrollBehavior`, changes in `params` or any
38+
transitions within its `children` will not adjust scroll position. This
39+
can be useful on a search page or in a tabbed interface.
3640

37-
Note that changes in `query` never adjust scroll position, regardless of the value of this attribute.
41+
Note that changes in `query` never adjust scroll position, regardless of
42+
the value of this attribute.
3843

3944
Example
4045
-------
@@ -55,5 +60,7 @@ Example
5560
</Route>
5661
```
5762

58-
[overview]:/docs/guides/overview.md
59-
[path-matching]:/docs/guides/path-matching.md
63+
[overview]:#TODO
64+
[path-matching]:#TODO
65+
[ignoreScrollBehavior]:#TODO
66+

0 commit comments

Comments
 (0)