You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/00 Guides/0 Overview.md
+26-38Lines changed: 26 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,30 +1,31 @@
1
+
## Overview
2
+
1
3
To illustrate the problems React Router is going to solve for you, let’s build a
2
4
small application without it.
3
5
4
-
Without React Router
5
-
--------------------
6
+
### Without React Router
6
7
7
8
```js
8
9
var About =React.createClass({/*...*/});
9
10
var Inbox =React.createClass({/*...*/});
10
11
var Home =React.createClass({/*...*/});
11
12
12
13
var App =React.createClass({
13
-
getInitialState() {
14
+
getInitialState() {
14
15
return {
15
16
route:window.location.hash.substr(1)
16
17
};
17
18
},
18
19
19
-
componentDidMount() {
20
+
componentDidMount() {
20
21
window.addEventListener('hashchange', () => {
21
22
this.setState({
22
23
route:window.location.hash.substr(1)
23
24
});
24
25
});
25
26
},
26
27
27
-
render() {
28
+
render() {
28
29
var Child;
29
30
switch (this.state.route) {
30
31
case'/about': Child = About; break;
@@ -48,12 +49,9 @@ var App = React.createClass({
48
49
React.render(<App />, document.body);
49
50
```
50
51
51
-
As the hash portion of the URL changes, `App` will render a different
52
-
`<Child/>` by branching on `this.state.route`. Pretty straightforward
53
-
stuff. But it gets complicated fast.
52
+
As the hash portion of the URL changes, `App` will render a different `<Child/>` by branching on `this.state.route`. Pretty straightforward stuff. But it gets complicated fast.
54
53
55
-
Imagine now that `Inbox` has some nested UI at different URLs, maybe
56
-
something like this master detail view:
54
+
Imagine now that `Inbox` has some nested UI at different URLs, maybe something like this master detail view:
57
55
58
56
```
59
57
path: /inbox/messages/1234
@@ -93,32 +91,28 @@ path: /inbox
93
91
+--------------+--------------------------------+
94
92
```
95
93
96
-
We'd have to make our url parsing a lot more intelligently, and end up
97
-
with a lot of code to figure out which branch of nested components to be
98
-
rendered at any given url: `App -> About`, `App -> Inbox -> Messages ->
99
-
Message`, `App -> Inbox -> Messages -> Stats`, etc.
94
+
We'd have to make our URL parsing a lot more intelligently, and end up with a lot of code to figure out which branch of nested components to be rendered at any given URL: `App -> About`, `App -> Inbox -> Messages -> Message`, `App -> Inbox -> Messages -> Stats`, etc.
100
95
101
-
With React Router
102
-
-----------------
96
+
### With React Router
103
97
104
-
Nested URLs and nested component hierarchy are at the heart of React
105
-
Router's declarative API. Lots of people like to use JSX to define their
106
-
routes, but you can use plain objects if you want.
98
+
Nested URLs and nested component hierarchy are at the heart of React Router's declarative API. Lots of people like to use JSX to define their routes, but you can use plain objects if you want.
107
99
108
100
Let's refactor our app to use React Router.
109
101
110
102
```js
111
103
// first we import some components
112
104
import { Router, Route, Link } from'react-router';
113
105
// the histories are imported separately for smaller builds
114
-
import { history } from'react-router/lib/HashHistory';
Alright, now we're ready to nest the inbox messages inside the inbox UI.
172
165
173
166
```js
174
167
// Make a new component to render inside of Inbox
175
168
var Message =React.createClass({
176
-
render() {
169
+
render() {
177
170
return<h3>Message</h3>;
178
171
}
179
172
});
180
173
181
174
var Inbox =React.createClass({
182
-
render() {
175
+
render() {
183
176
return (
184
177
<div>
185
178
<h2>Inbox</h2>
@@ -203,20 +196,15 @@ React.render((
203
196
), document.body);
204
197
```
205
198
206
-
Now visits to urls like `inbox/messages/Jkei3c32` will match the new
207
-
route and nest the UI branch of `App -> Inbox -> Message`.
199
+
Now visits to URLs like `inbox/messages/Jkei3c32` will match the new route and nest the UI branch of `App -> Inbox -> Message`.
208
200
209
-
Getting the url parameters
210
-
--------------------------
201
+
### Getting the URL parameters
211
202
212
-
We're going to need to know something about the message in order to
213
-
fetch it from the server. Route components get some useful properties
214
-
injected into them when you render, particularly the parameters from the
215
-
dynamic segment of your path. In our case, `:id`.
203
+
We're going to need to know something about the message in order to fetch it from the server. Route components get some useful properties injected into them when you render, particularly the parameters from the dynamic segment of your path. In our case, `:id`.
0 commit comments