Skip to content

Commit bf27e2f

Browse files
committed
Update Overview
1 parent a657398 commit bf27e2f

File tree

1 file changed

+26
-38
lines changed

1 file changed

+26
-38
lines changed

doc/00 Guides/0 Overview.md

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
1+
## Overview
2+
13
To illustrate the problems React Router is going to solve for you, let’s build a
24
small application without it.
35

4-
Without React Router
5-
--------------------
6+
### Without React Router
67

78
```js
89
var About = React.createClass({/*...*/});
910
var Inbox = React.createClass({/*...*/});
1011
var Home = React.createClass({/*...*/});
1112

1213
var App = React.createClass({
13-
getInitialState () {
14+
getInitialState() {
1415
return {
1516
route: window.location.hash.substr(1)
1617
};
1718
},
1819

19-
componentDidMount () {
20+
componentDidMount() {
2021
window.addEventListener('hashchange', () => {
2122
this.setState({
2223
route: window.location.hash.substr(1)
2324
});
2425
});
2526
},
2627

27-
render () {
28+
render() {
2829
var Child;
2930
switch (this.state.route) {
3031
case '/about': Child = About; break;
@@ -48,12 +49,9 @@ var App = React.createClass({
4849
React.render(<App />, document.body);
4950
```
5051

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.
5453

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:
5755

5856
```
5957
path: /inbox/messages/1234
@@ -93,32 +91,28 @@ path: /inbox
9391
+--------------+--------------------------------+
9492
```
9593

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.
10095

101-
With React Router
102-
-----------------
96+
### With React Router
10397

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.
10799

108100
Let's refactor our app to use React Router.
109101

110102
```js
111103
// first we import some components
112104
import { Router, Route, Link } from 'react-router';
113105
// the histories are imported separately for smaller builds
114-
import { history } from 'react-router/lib/HashHistory';
106+
import createHistory from 'history/lib/createHashHistory';
107+
108+
var history = createHistory();
115109

116110
// ...
117111

118112
// then we delete a bunch of code from `App` and add some `Link`
119113
// components
120114
var App = React.createClass({
121-
render () {
115+
render() {
122116
return (
123117
<div>
124118
<h1>App</h1>
@@ -138,13 +132,13 @@ var App = React.createClass({
138132
}
139133
});
140134

141-
// Finally we render a `Router` component with some `Route`s, it'll do all
142-
// the fancy routing stuff for us.
135+
// Finally we render a Router component with some Routes.
136+
// It does all the fancy routing stuff for us.
143137
React.render((
144138
<Router history={history}>
145139
<Route path="/" component={App}>
146-
<Route path="about" component={About}/>
147-
<Route path="inbox" component={Inbox}/>
140+
<Route path="about" component={About} />
141+
<Route path="inbox" component={Inbox} />
148142
</Route>
149143
</Router>
150144
), document.body);
@@ -165,21 +159,20 @@ var routes = {
165159
React.render(<Router history={history} children={routes}/>, document.body);
166160
```
167161

168-
Adding more UI
169-
--------------
162+
### Adding more UI
170163

171164
Alright, now we're ready to nest the inbox messages inside the inbox UI.
172165

173166
```js
174167
// Make a new component to render inside of Inbox
175168
var Message = React.createClass({
176-
render () {
169+
render() {
177170
return <h3>Message</h3>;
178171
}
179172
});
180173

181174
var Inbox = React.createClass({
182-
render () {
175+
render() {
183176
return (
184177
<div>
185178
<h2>Inbox</h2>
@@ -203,20 +196,15 @@ React.render((
203196
), document.body);
204197
```
205198

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`.
208200

209-
Getting the url parameters
210-
--------------------------
201+
### Getting the URL parameters
211202

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`.
216204

217205
```js
218206
var Message = React.createClass({
219-
componentDidMount: function () {
207+
componentDidMount() {
220208
// from the path `/inbox/messages/:id`
221209
var id = this.props.params.id;
222210
fetchMessage(id, function (err, message) {

0 commit comments

Comments
 (0)