Skip to content

Commit dd7bcc8

Browse files
committed
Update Introduction.md
Updated the docs to use `classes` and `ReactDOM`. This allows the user to get the most updated approached to build React with react-router
1 parent f5aa145 commit dd7bcc8

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

docs/Introduction.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,28 @@ To illustrate the problems React Router is going to solve for you, let's build a
77
### Without React Router
88

99
```js
10-
import React from 'react'
10+
import React, { Component } from 'react'
11+
import ReactDOM from 'react-dom'
1112

1213
const About = React.createClass({/*...*/})
1314
const Inbox = React.createClass({/*...*/})
1415
const Home = React.createClass({/*...*/})
1516

16-
const App = React.createClass({
17-
getInitialState() {
18-
return {
17+
class App extends Component {
18+
constructor() {
19+
this.state = {
1920
route: window.location.hash.substr(1)
2021
}
21-
},
22-
22+
}
23+
2324
componentDidMount() {
2425
window.addEventListener('hashchange', () => {
2526
this.setState({
2627
route: window.location.hash.substr(1)
2728
})
2829
})
29-
},
30-
30+
}
31+
3132
render() {
3233
let Child
3334
switch (this.state.route) {
@@ -47,9 +48,9 @@ const App = React.createClass({
4748
</div>
4849
)
4950
}
50-
})
51+
}
5152

52-
React.render(<App />, document.body)
53+
ReactDOM.render(<App />, document.body)
5354
```
5455

5556
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.
@@ -101,14 +102,15 @@ We'd have to make our URL parsing a lot smarter, and we would end up with a lot
101102
Let's refactor our app to use React Router.
102103

103104
```js
104-
import React from 'react'
105+
import ReactDOM from 'react-dom'
106+
import React, { Component } from 'react'
105107

106108
// First we import some components...
107109
import { Router, Route, Link } from 'react-router'
108110

109111
// Then we delete a bunch of code from App and
110112
// add some <Link> elements...
111-
const App = React.createClass({
113+
class App extends Component {
112114
render() {
113115
return (
114116
<div>
@@ -127,11 +129,11 @@ const App = React.createClass({
127129
</div>
128130
)
129131
}
130-
})
132+
}
131133

132134
// Finally, we render a <Router> with some <Route>s.
133135
// It does all the fancy routing stuff for us.
134-
React.render((
136+
ReactDOM.render((
135137
<Router>
136138
<Route path="/" component={App}>
137139
<Route path="about" component={About} />
@@ -153,7 +155,7 @@ const routes = {
153155
]
154156
}
155157

156-
React.render(<Router routes={routes} />, document.body)
158+
ReactDOM.render(<Router routes={routes} />, document.body)
157159
```
158160

159161
## Adding More UI
@@ -162,13 +164,13 @@ Alright, now we're ready to nest the inbox messages inside the inbox UI.
162164

163165
```js
164166
// Make a new component to render inside of Inbox
165-
const Message = React.createClass({
167+
class Message extends Component {
166168
render() {
167169
return <h3>Message</h3>
168170
}
169-
})
171+
}
170172

171-
const Inbox = React.createClass({
173+
class Inbox extends Component {
172174
render() {
173175
return (
174176
<div>
@@ -178,9 +180,9 @@ const Inbox = React.createClass({
178180
</div>
179181
)
180182
}
181-
})
183+
}
182184

183-
React.render((
185+
ReactDOM.render((
184186
<Router>
185187
<Route path="/" component={App}>
186188
<Route path="about" component={About} />
@@ -200,7 +202,7 @@ Now visits to URLs like `inbox/messages/Jkei3c32` will match the new route and n
200202
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`.
201203

202204
```js
203-
const Message = React.createClass({
205+
class Message extends Component
204206

205207
componentDidMount() {
206208
// from the path `/inbox/messages/:id`

0 commit comments

Comments
 (0)