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: content/docs/state-and-lifecycle.md
+23Lines changed: 23 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,7 @@ This page introduces the concept of state and lifecycle in a React component. Yo
14
14
15
15
Consider the ticking clock example from [one of the previous sections](/docs/rendering-elements.html#updating-the-rendered-element). In [Rendering Elements](/docs/rendering-elements.html#rendering-an-element-into-the-dom), we have only learned one way to update the UI. We call `ReactDOM.render()` to change the rendered output:
16
16
[이전 섹션](/docs/rendering-elements.html#updating-the-rendered-element)에서 다뤄본 째깍거리는 시계 예시를 다시 살펴보겠습니다. [엘리먼트 렌더링](/docs/rendering-elements.html#rendering-an-element-into-the-dom)에서는 UI를 업데이트하는 한 가지 방법만 배웠으며, 렌더링 된 출력값을 변경하기 위해 `ReactDOM.render()`를 호출했습니다.
17
+
17
18
```js{8-11}
18
19
function tick() {
19
20
const element = (
@@ -39,6 +40,7 @@ In this section, we will learn how to make the `Clock` component truly reusable
39
40
40
41
We can start by encapsulating how the clock looks:
41
42
시계가 생긴 것에 따라 캡슐화하는 것으로 시작할 수 있습니다.
43
+
42
44
```js{3-6,12}
43
45
function Clock(props) {
44
46
return (
@@ -67,6 +69,7 @@ However, it misses a crucial requirement: the fact that the `Clock` sets up a ti
67
69
68
70
Ideally we want to write this once and have the `Clock` update itself:
69
71
이상적으로 한 번만 코드를 작성하고 `Clock`이 스스로 업데이트하도록 만들려고 합니다.
72
+
70
73
```js{2}
71
74
ReactDOM.render(
72
75
<Clock />,
@@ -104,6 +107,7 @@ You can convert a function component like `Clock` to a class in five steps:
104
107
105
108
5. Delete the remaining empty function declaration.
106
109
5. 남아있는 빈 함수 선언을 삭제합니다.
110
+
107
111
```js
108
112
classClockextendsReact.Component {
109
113
render() {
@@ -134,6 +138,7 @@ We will move the `date` from props to state in three steps:
134
138
135
139
1) Replace `this.props.date` with `this.state.date` in the `render()` method:
136
140
1)`render()` 메서드 안에 있는 `this.props.date`를 `this.state.date`로 변경합니다.
141
+
137
142
```js{6}
138
143
class Clock extends React.Component {
139
144
render() {
@@ -149,6 +154,7 @@ class Clock extends React.Component {
149
154
150
155
2) Add a [클래스 constructor](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes#Constructor) that assigns the initial `this.state`:
151
156
2) 초기 `this.state`를 지정하는 [class constructor](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes#Constructor)를 추가합니다.
157
+
152
158
```js{4}
153
159
class Clock extends React.Component {
154
160
constructor(props) {
@@ -169,6 +175,7 @@ class Clock extends React.Component {
169
175
170
176
Note how we pass `props` to the base constructor:
171
177
여기서 어떻게 `props`를 기본 constructor에 전달하는지 유의하십시오.
178
+
172
179
```js{2}
173
180
constructor(props) {
174
181
super(props);
@@ -181,6 +188,7 @@ Class components should always call the base constructor with `props`.
181
188
182
189
3) Remove the `date` prop from the `<Clock />` element:
183
190
3)`<Clock />` 요소에서 `date` prop을 삭제합니다.
191
+
184
192
```js{2}
185
193
ReactDOM.render(
186
194
<Clock />,
@@ -193,6 +201,7 @@ We will later add the timer code back to the component itself.
193
201
194
202
The result looks like this:
195
203
결과는 다음과 같습니다.
204
+
196
205
```js{2-5,11,18}
197
206
class Clock extends React.Component {
198
207
constructor(props) {
@@ -236,6 +245,7 @@ We also want to [clear that timer](https://developer.mozilla.org/en-US/docs/Web/
236
245
237
246
We can declare special methods on the component class to run some code when a component mounts and unmounts:
238
247
컴포넌트 클래스에서 특별한 메서드를 선언하여 컴포넌트가 마운트되거나 언마운트 될 때 일부 코드를 작동할 수 있습니다.
248
+
239
249
```js{7-9,11-13}
240
250
class Clock extends React.Component {
241
251
constructor(props) {
@@ -267,6 +277,7 @@ These methods are called "lifecycle methods".
267
277
268
278
The `componentDidMount()` method runs after the component output has been rendered to the DOM. This is a good place to set up a timer:
269
279
`componentDidMount()` 메서드는 컴포넌트 출력물이 DOM에 렌더링 된 후에 실행됩니다. 이 장소가 타이머를 설정하기에 가장 좋아 보입니다.
280
+
270
281
```js{2-5}
271
282
componentDidMount() {
272
283
this.timerID = setInterval(
@@ -284,6 +295,7 @@ While `this.props` is set up by React itself and `this.state` has a special mean
284
295
285
296
We will tear down the timer in the `componentWillUnmount()` lifecycle method:
286
297
`componentWillUnmount()` 생명주기 메서드 안에 있는 타이머를 분해해 보겠습니다.
298
+
287
299
```js{2}
288
300
componentWillUnmount() {
289
301
clearInterval(this.timerID);
@@ -294,6 +306,7 @@ Finally, we will implement a method called `tick()` that the `Clock` component w
294
306
마지막으로 `Clock` 컴포넌트가 매초 작동하도록 하는 `tick()`이라는 메서드를 구현해 보겠습니다.
295
307
It will use `this.setState()` to schedule updates to the component local state:
296
308
이것은 컴포넌트 로컬 state를 업데이트하기 위해 `this.setState()`를 사용합니다.
309
+
297
310
```js{18-22}
298
311
class Clock extends React.Component {
299
312
constructor(props) {
@@ -377,6 +390,7 @@ this.state.comment = 'Hello';
377
390
378
391
Instead, use `setState()`:
379
392
대신에 `setState()`를 사용합니다.
393
+
380
394
```js
381
395
// Correct
382
396
this.setState({comment:'Hello'});
@@ -396,6 +410,7 @@ Because `this.props` and `this.state` may be updated asynchronously, you should
396
410
397
411
For example, this code may fail to update the counter:
398
412
예를 들어 다음 코드는 카운터 업데이트에 실패할 수 있습니다.
413
+
399
414
```js
400
415
// Wrong
401
416
this.setState({
@@ -405,6 +420,7 @@ this.setState({
405
420
406
421
To fix it, use a second form of `setState()` that accepts a function rather than an object. That function will receive the previous state as the first argument, and the props at the time the update is applied as the second argument:
407
422
이를 수정하기 위해 객체보다는 함수를 인자로 사용하는 다른 형태의 `setState()`를 사용합니다. 그 함수는 이전 state를 첫 번째 인자로 받아들일 것이고, 업데이트가 적용된 시점의 props를 두 번째 인자로 받아들일 것입니다.
We used an [arrow function](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions) above, but it also works with regular functions:
416
432
위에서는 [화살표 함수](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions)를 사용했지만, 일반적인 함수에서도 정상적으로 작동합니다.
433
+
417
434
```js
418
435
// Correct
419
436
this.setState(function(state, props) {
@@ -431,6 +448,7 @@ When you call `setState()`, React merges the object you provide into the current
431
448
432
449
For example, your state may contain several independent variables:
433
450
예를 들어, state는 다양한 독립적인 변수를 포함할 수 있습니다.
451
+
434
452
```js{4,5}
435
453
constructor(props) {
436
454
super(props);
@@ -443,6 +461,7 @@ For example, your state may contain several independent variables:
443
461
444
462
Then you can update them independently with separate `setState()` calls:
445
463
별도의 `setState()` 호출로 이러한 변수를 독립적으로 업데이트할 수 있습니다.
464
+
446
465
```js{4,10}
447
466
componentDidMount() {
448
467
fetchPosts().then(response => {
@@ -473,18 +492,21 @@ This is why state is often called local or encapsulated. It is not accessible to
473
492
474
493
A component may choose to pass its state down as props to its child components:
475
494
컴포넌트는 자신의 state를 자식 컴포넌트에 props로 전달할 수 있습니다.
495
+
476
496
```js
477
497
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
478
498
```
479
499
480
500
This also works for user-defined components:
481
501
이것 또한 사용자 정의된 컴포넌트에도 적용 가능합니다.
502
+
482
503
```js
483
504
<FormattedDate date={this.state.date} />
484
505
```
485
506
486
507
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:
487
508
`FormattedDate` 컴포넌트는 `date`를 자신의 props로 받을 것이고 이것이 `Clock`의 state로부터 왔는지, `Clock`의 props에서 왔는지, 수동으로 입력한 것인지 알지 못합니다.
509
+
488
510
```js
489
511
functionFormattedDate(props) {
490
512
return<h2>It is {props.date.toLocaleTimeString()}.</h2>;
@@ -502,6 +524,7 @@ If you imagine a component tree as a waterfall of props, each component's state
502
524
503
525
To show that all components are truly isolated, we can create an `App` component that renders three `<Clock>`s:
504
526
모든 컴포넌트가 완전히 독립적이라는 것을 보여주기 위해 `App`와 렌더링하는 세 개의 `<Clock>`을 만들었습니다.
0 commit comments