@@ -248,7 +248,7 @@ class Clock extends React.Component {
248
248
249
249
注意我們是如何正確的在 ` this ` 保存 timer ID。
250
250
251
- 雖然 ` this.props ` 是由 React 本身設定的,而且 ` this.state ` 具有特殊的意義,如果你需要儲存一些不相關於 data flow 的內容 (像是 timer ID),你可以自由的手動加入。
251
+ 雖然 ` this.props ` 是由 React 本身設定的,而且 ` this.state ` 具有特殊的意義,如果你需要儲存一些不相關於資料流的內容 (像是 timer ID),你可以自由的手動加入。
252
252
253
253
我們將會在 ` componentWillUnmount() ` 生命週期方法內移除 timer:
254
254
@@ -327,18 +327,18 @@ ReactDOM.render(
327
327
例如,這將不會重新 render component:
328
328
329
329
``` js
330
- // Wrong
330
+ // 錯誤
331
331
this .state .comment = ' Hello' ;
332
332
```
333
333
334
334
相反的,使用 ` setState() ` :
335
335
336
336
``` js
337
- // Correct
337
+ // 正確
338
338
this .setState ({comment: ' Hello' });
339
339
```
340
340
341
- 你唯一可以指定 ` this.state ` 值的地方是在 construcotr 。
341
+ 你唯一可以指定 ` this.state ` 值的地方是在 constructor 。
342
342
343
343
### State 的更新可能是非同步的 {#state-updates-may-be-asynchronous}
344
344
@@ -349,7 +349,7 @@ React 可以將多個 `setState()` 呼叫批次處理為單一的更新,以提
349
349
例如,這個程式碼可能無法更新 counter:
350
350
351
351
``` js
352
- // Wrong
352
+ // 錯誤
353
353
this .setState ({
354
354
counter: this .state .counter + this .props .increment ,
355
355
});
@@ -358,7 +358,7 @@ this.setState({
358
358
要修正這個問題,使用第二種形式的 ` setState() ` ,它接受一個 function 而不是一個 object。Function 將接收先前的 state 作為第一個參數,並且將更新的 props 作為第二個參數:
359
359
360
360
``` js
361
- // Correct
361
+ // 正確
362
362
this .setState ((state , props ) => ({
363
363
counter: state .counter + props .increment
364
364
}));
@@ -367,7 +367,7 @@ this.setState((state, props) => ({
367
367
在上面我們使用 [ arrow function] ( https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions ) ,但它也可以適用於正常的 function:
368
368
369
369
``` js
370
- // Correct
370
+ // 正確
371
371
this .setState (function (state , props ) {
372
372
return {
373
373
counter: state .counter + props .increment
@@ -411,7 +411,7 @@ this.setState(function(state, props) {
411
411
412
412
這個 merge 是 shallow 的,所以 ` this.setState({comments}) ` 保持 ` this.state.posts ` 的完整,但它完全取代了 ` this.state.comments ` 。
413
413
414
- ## 資料流向下 {#the-data-flows-down}
414
+ ## 向下資料流 {#the-data-flows-down}
415
415
416
416
Parent 和 child component 不會知道某個 component 是 stateful 或 stateless 的 component,而且它們不在意它是透過 function 或是 class 被定義的。
417
417
0 commit comments