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: docs/Introduction.md
+23-21Lines changed: 23 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,27 +7,28 @@ To illustrate the problems React Router is going to solve for you, let's build a
7
7
### Without React Router
8
8
9
9
```js
10
-
importReactfrom'react'
10
+
importReact, { Component } from'react'
11
+
importReactDOMfrom'react-dom'
11
12
12
13
constAbout=React.createClass({/*...*/})
13
14
constInbox=React.createClass({/*...*/})
14
15
constHome=React.createClass({/*...*/})
15
16
16
-
constApp=React.createClass({
17
-
getInitialState() {
18
-
return {
17
+
classAppextendsComponent{
18
+
constructor() {
19
+
this.state= {
19
20
route:window.location.hash.substr(1)
20
21
}
21
-
},
22
-
22
+
}
23
+
23
24
componentDidMount() {
24
25
window.addEventListener('hashchange', () => {
25
26
this.setState({
26
27
route:window.location.hash.substr(1)
27
28
})
28
29
})
29
-
},
30
-
30
+
}
31
+
31
32
render() {
32
33
let Child
33
34
switch (this.state.route) {
@@ -47,9 +48,9 @@ const App = React.createClass({
47
48
</div>
48
49
)
49
50
}
50
-
})
51
+
}
51
52
52
-
React.render(<App />, document.body)
53
+
ReactDOM.render(<App />, document.body)
53
54
```
54
55
55
56
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
@@ -200,7 +202,7 @@ Now visits to URLs like `inbox/messages/Jkei3c32` will match the new route and n
200
202
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