Skip to content

Commit 4b1ec7a

Browse files
author
Ives van Hoorne
committed
Convert state-and-lifecycle to inline embeds
1 parent 1bb4d8e commit 4b1ec7a

File tree

14 files changed

+206
-149
lines changed

14 files changed

+206
-149
lines changed

content/docs/state-and-lifecycle.md

Lines changed: 7 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,13 @@ So far we have only learned one way to update the UI.
1313

1414
We call `ReactDOM.render()` to change the rendered output:
1515

16-
```js{8-11}
17-
function tick() {
18-
const element = (
19-
<div>
20-
<h1>Hello, world!</h1>
21-
<h2>It is {new Date().toLocaleTimeString()}.</h2>
22-
</div>
23-
);
24-
ReactDOM.render(
25-
element,
26-
document.getElementById('root')
27-
);
28-
}
29-
30-
setInterval(tick, 1000);
31-
```
32-
33-
[Try it on CodePen.](http://codepen.io/gaearon/pen/gwoJZk?editors=0010)
16+
[Run example.](source:examples/single-file-examples/src/state-and-lifecycle/state-and-lifecycle-1.js{11})
3417

3518
In this section, we will learn how to make the `Clock` component truly reusable and encapsulated. It will set up its own timer and update itself every second.
3619

3720
We can start by encapsulating how the clock looks:
3821

39-
```js{3-6,12}
40-
function Clock(props) {
41-
return (
42-
<div>
43-
<h1>Hello, world!</h1>
44-
<h2>It is {props.date.toLocaleTimeString()}.</h2>
45-
</div>
46-
);
47-
}
48-
49-
function tick() {
50-
ReactDOM.render(
51-
<Clock date={new Date()} />,
52-
document.getElementById('root')
53-
);
54-
}
55-
56-
setInterval(tick, 1000);
57-
```
58-
59-
[Try it on CodePen.](http://codepen.io/gaearon/pen/dpdoYR?editors=0010)
22+
[Run example.](source:examples/single-file-examples/src/state-and-lifecycle/state-and-lifecycle-2.js{6,7,8,9,14})
6023

6124
However, it misses a crucial requirement: the fact that the `Clock` sets up a timer and updates the UI every second should be an implementation detail of the `Clock`.
6225

@@ -89,20 +52,7 @@ You can convert a functional component like `Clock` to a class in five steps:
8952

9053
5. Delete the remaining empty function declaration.
9154

92-
```js
93-
class Clock extends React.Component {
94-
render() {
95-
return (
96-
<div>
97-
<h1>Hello, world!</h1>
98-
<h2>It is {this.props.date.toLocaleTimeString()}.</h2>
99-
</div>
100-
);
101-
}
102-
}
103-
```
104-
105-
[Try it on CodePen.](http://codepen.io/gaearon/pen/zKRGpo?editors=0010)
55+
[Run example.](source:examples/single-file-examples/src/state-and-lifecycle/state-and-lifecycle-3.js)
10656

10757
`Clock` is now defined as a class rather than a function.
10858

@@ -171,30 +121,7 @@ We will later add the timer code back to the component itself.
171121

172122
The result looks like this:
173123

174-
```js{2-5,11,18}
175-
class Clock extends React.Component {
176-
constructor(props) {
177-
super(props);
178-
this.state = {date: new Date()};
179-
}
180-
181-
render() {
182-
return (
183-
<div>
184-
<h1>Hello, world!</h1>
185-
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
186-
</div>
187-
);
188-
}
189-
}
190-
191-
ReactDOM.render(
192-
<Clock />,
193-
document.getElementById('root')
194-
);
195-
```
196-
197-
[Try it on CodePen.](http://codepen.io/gaearon/pen/KgQpJd?editors=0010)
124+
[Run example.](source:examples/single-file-examples/src/state-and-lifecycle/state-and-lifecycle-4.js{5,6,7,8,14,20})
198125

199126
Next, we'll make the `Clock` set up its own timer and update itself every second.
200127

@@ -265,47 +192,7 @@ Finally, we will implement a method called `tick()` that the `Clock` component w
265192

266193
It will use `this.setState()` to schedule updates to the component local state:
267194

268-
```js{18-22}
269-
class Clock extends React.Component {
270-
constructor(props) {
271-
super(props);
272-
this.state = {date: new Date()};
273-
}
274-
275-
componentDidMount() {
276-
this.timerID = setInterval(
277-
() => this.tick(),
278-
1000
279-
);
280-
}
281-
282-
componentWillUnmount() {
283-
clearInterval(this.timerID);
284-
}
285-
286-
tick() {
287-
this.setState({
288-
date: new Date()
289-
});
290-
}
291-
292-
render() {
293-
return (
294-
<div>
295-
<h1>Hello, world!</h1>
296-
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
297-
</div>
298-
);
299-
}
300-
}
301-
302-
ReactDOM.render(
303-
<Clock />,
304-
document.getElementById('root')
305-
);
306-
```
307-
308-
[Try it on CodePen.](http://codepen.io/gaearon/pen/amqdNA?editors=0010)
195+
[Run example.](source:examples/single-file-examples/src/state-and-lifecycle/state-and-lifecycle-5.js{18,19,20,21,22})
309196

310197
Now the clock ticks every second.
311198

@@ -434,38 +321,15 @@ This also works for user-defined components:
434321

435322
The `FormattedDate` component would receive the `date` in its props and wouldn't know whether it came from the `Clock`'s state, from the `Clock`'s props, or was typed by hand:
436323

437-
```js
438-
function FormattedDate(props) {
439-
return <h2>It is {props.date.toLocaleTimeString()}.</h2>;
440-
}
441-
```
442-
443-
[Try it on CodePen.](http://codepen.io/gaearon/pen/zKRqNB?editors=0010)
324+
[Run example.](source:examples/single-file-examples/src/state-and-lifecycle/state-and-lifecycle-6.js{4,5,6})
444325

445326
This is commonly called a "top-down" or "unidirectional" data flow. Any state is always owned by some specific component, and any data or UI derived from that state can only affect components "below" them in the tree.
446327

447328
If you imagine a component tree as a waterfall of props, each component's state is like an additional water source that joins it at an arbitrary point but also flows down.
448329

449330
To show that all components are truly isolated, we can create an `App` component that renders three `<Clock>`s:
450331

451-
```js{4-6}
452-
function App() {
453-
return (
454-
<div>
455-
<Clock />
456-
<Clock />
457-
<Clock />
458-
</div>
459-
);
460-
}
461-
462-
ReactDOM.render(
463-
<App />,
464-
document.getElementById('root')
465-
);
466-
```
467-
468-
[Try it on CodePen.](http://codepen.io/gaearon/pen/vXdGmd?editors=0010)
332+
[Run example.](source:examples/single-file-examples/src/state-and-lifecycle/state-and-lifecycle-7/index.js{9,10,11})
469333

470334
Each `Clock` sets up its own timer and updates independently.
471335

content/docs/uncontrolled-components.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ To write an uncontrolled component, instead of writing an event handler for ever
1010

1111
For example, this code accepts a single name in an uncontrolled component:
1212

13-
[Run the example.](source:examples/uncontrolled-components/src/index.js{11,20}?editorsize=70)
13+
[Run the example.](source:examples/single-file-examples/src/uncontrolled-components.js{11,20}?editorsize=70)
1414

1515
Since an uncontrolled component keeps the source of truth in the DOM, it is sometimes easier to integrate React and non-React code when using uncontrolled components. It can also be slightly less code if you want to be quick and dirty. Otherwise, you should usually use controlled components.
1616

examples/uncontrolled-components/package.json renamed to examples/single-file-examples/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "uncontrolled-components",
2+
"name": "react-doc-examples",
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
4+
function tick() {
5+
const element = (
6+
<div>
7+
<h1>Hello, world!</h1>
8+
<h2>It is {new Date().toLocaleTimeString()}.</h2>
9+
</div>
10+
);
11+
ReactDOM.render(element, document.getElementById('root'));
12+
}
13+
14+
setInterval(tick, 1000);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
4+
function Clock(props) {
5+
return (
6+
<div>
7+
<h1>Hello, world!</h1>
8+
<h2>It is {props.date.toLocaleTimeString()}.</h2>
9+
</div>
10+
);
11+
}
12+
13+
function tick() {
14+
ReactDOM.render(<Clock date={new Date()} />, document.getElementById('root'));
15+
}
16+
17+
setInterval(tick, 1000);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
4+
class Clock extends React.Component {
5+
render() {
6+
return (
7+
<div>
8+
<h1>Hello, world!</h1>
9+
<h2>It is {this.props.date.toLocaleTimeString()}.</h2>
10+
</div>
11+
);
12+
}
13+
}
14+
15+
function tick() {
16+
ReactDOM.render(<Clock date={new Date()} />, document.getElementById('root'));
17+
}
18+
19+
setInterval(tick, 1000);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
4+
class Clock extends React.Component {
5+
constructor(props) {
6+
super(props);
7+
this.state = {date: new Date()};
8+
}
9+
10+
render() {
11+
return (
12+
<div>
13+
<h1>Hello, world!</h1>
14+
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
15+
</div>
16+
);
17+
}
18+
}
19+
20+
ReactDOM.render(<Clock />, document.getElementById('root'));
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
4+
class Clock extends React.Component {
5+
constructor(props) {
6+
super(props);
7+
this.state = {date: new Date()};
8+
}
9+
10+
componentDidMount() {
11+
this.timerID = setInterval(() => this.tick(), 1000);
12+
}
13+
14+
componentWillUnmount() {
15+
clearInterval(this.timerID);
16+
}
17+
18+
tick() {
19+
this.setState({
20+
date: new Date(),
21+
});
22+
}
23+
24+
render() {
25+
return (
26+
<div>
27+
<h1>Hello, world!</h1>
28+
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
29+
</div>
30+
);
31+
}
32+
}
33+
34+
ReactDOM.render(<Clock />, document.getElementById('root'));
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
4+
function FormattedDate(props) {
5+
return <h2>It is {props.date.toLocaleTimeString()}.</h2>;
6+
}
7+
8+
class Clock extends React.Component {
9+
constructor(props) {
10+
super(props);
11+
this.state = {date: new Date()};
12+
}
13+
14+
componentDidMount() {
15+
this.timerID = setInterval(() => this.tick(), 1000);
16+
}
17+
18+
componentWillUnmount() {
19+
clearInterval(this.timerID);
20+
}
21+
22+
tick() {
23+
this.setState({
24+
date: new Date(),
25+
});
26+
}
27+
28+
render() {
29+
return (
30+
<div>
31+
<h1>Hello, world!</h1>
32+
<FormattedDate date={this.state.date} />
33+
</div>
34+
);
35+
}
36+
}
37+
38+
ReactDOM.render(<Clock />, document.getElementById('root'));
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React from 'react';
2+
3+
function FormattedDate(props) {
4+
return <h2>It is {props.date.toLocaleTimeString()}.</h2>;
5+
}
6+
7+
export default class Clock extends React.Component {
8+
constructor(props) {
9+
super(props);
10+
this.state = {date: new Date()};
11+
}
12+
13+
componentDidMount() {
14+
this.timerID = setInterval(() => this.tick(), 1000);
15+
}
16+
17+
componentWillUnmount() {
18+
clearInterval(this.timerID);
19+
}
20+
21+
tick() {
22+
this.setState({
23+
date: new Date(),
24+
});
25+
}
26+
27+
render() {
28+
return (
29+
<div>
30+
<h1>Hello, world!</h1>
31+
<FormattedDate date={this.state.date} />
32+
</div>
33+
);
34+
}
35+
}

0 commit comments

Comments
 (0)