Skip to content

Commit 7b3a3e6

Browse files
authored
Merge pull request #45 from Chocobo1/typo
Fix typos & improvements
2 parents 71b09c6 + 6a7cf30 commit 7b3a3e6

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

Ch04/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Ch04 Props/State 基礎與 Component 生命週期
1+
# Ch04 Props/State 基礎與 Component 生命週期
22

33
1. [Props、State、Refs 與表單處理](https://github.com/kdchang/reactjs101/blob/master/Ch04/props-state-introduction.md)
44
2. [React Component 規格與生命週期(Life Cycle)](https://github.com/kdchang/reactjs101/blob/master/Ch04/react-component-life-cycle.md)

Ch04/props-state-introduction.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ app.js,使用 ES6 Class Component 寫法:
3232
class HelloMessage extends React.Component {
3333
// 若是需要綁定 this.方法或是需要在 constructor 使用 props,定義 state,就需要 constructor。若是在其他方法(如 render)使用 this.props 則不用一定要定義 constructor
3434
constructor(props) {
35-
// 對於 OOP 物件導向程式設計熟悉的讀者應該對於 constructor 建構子的使用不陌生,事實上它是 ES6 的語法糖,骨子裡還是 portotype based 物件導向程式語言。透過 extends 可以繼承 React.Component 父類別。super 方法可以呼叫繼承父類別的建構子
35+
// 對於 OOP 物件導向程式設計熟悉的讀者應該對於 constructor 建構子的使用不陌生,事實上它是 ES6 的語法糖,骨子裡還是 prototype based 物件導向程式語言。透過 extends 可以繼承 React.Component 父類別。super 方法可以呼叫繼承父類別的建構子
3636
super(props);
3737
this.state = {}
3838
}
@@ -51,7 +51,7 @@ HelloMessage.propTypes = {
5151

5252
// Prop 預設值,若對應 props 沒傳入值將會使用 default 值 Zuck
5353
HelloMessage.defaultProps = {
54-
name: 'Zuck',
54+
name: 'Zuck',
5555
}
5656

5757
ReactDOM.render(<HelloMessage name="Mark" />, document.getElementById('app'));
@@ -62,7 +62,7 @@ ReactDOM.render(<HelloMessage name="Mark" />, document.getElementById('app'));
6262
使用 Functional Component 寫法:
6363

6464
```javascript
65-
// Functional Component 可以視為 f(d) => UI,根據傳進去的 props 繪出對應的 UI。注意這邊 props 是傳入函式的參數因此取用 props 不用加 this
65+
// Functional Component 可以視為 f(d) => UI,根據傳進去的 props 繪出對應的 UI。注意這邊 props 是傳入函式的參數因此取用 props 不用加 this
6666
const HelloMessage = (props) => (
6767
<div>Hello {props.name}</div>
6868
);
@@ -74,7 +74,7 @@ HelloMessage.propTypes = {
7474

7575
// Prop 預設值,若對應 props 沒傳入值將會使用 default 值 Zuck。用法等於 ES5 的 getDefaultProps
7676
HelloMessage.defaultProps = {
77-
name: 'Zuck',
77+
name: 'Zuck',
7878
}
7979

8080
ReactDOM.render(<HelloMessage name="Mark" />, document.getElementById('app'));
@@ -112,7 +112,7 @@ app.js:
112112
class Timer extends React.Component {
113113
constructor(props) {
114114
super(props);
115-
// 與 ES5 React.createClass({}) 不同的是 component 內自定義的方法需要自行綁定 this context,或是使用 arrow function
115+
// 與 ES5 React.createClass({}) 不同的是 component 內自定義的方法需要自行綁定 this context,或是使用 arrow function
116116
this.tick = this.tick.bind(this);
117117
// 初始 state,等於 ES5 中的 getInitialState
118118
this.state = {
@@ -127,15 +127,15 @@ class Timer extends React.Component {
127127
componentDidMount() {
128128
this.interval = setInterval(this.tick, 1000);
129129
}
130-
// componentWillUnmount 為 component 生命週期中 component 即將移出插入的節點的階段。這邊移除了 setInterval 效力
130+
// componentWillUnmount 為 component 生命週期中 component 即將移出插入的節點的階段。這邊移除了 setInterval 效力
131131
componentWillUnmount() {
132132
clearInterval(this.interval);
133133
}
134134
// render 為 class Component 中唯一需要定義的方法,其回傳 component 欲顯示的內容
135135
render() {
136136
return (
137137
<div>Seconds Elapsed: {this.state.secondsElapsed}</div>
138-
);
138+
);
139139
}
140140
}
141141

@@ -192,7 +192,7 @@ class TodoApp extends React.Component {
192192
}
193193
}
194194
onChange(e) {
195-
this.setState({text: e.target.value});
195+
this.setState({text: e.target.value});
196196
}
197197
handleSubmit(e) {
198198
e.preventDefault();
@@ -258,10 +258,10 @@ class MarkdownEditor extends React.Component {
258258
handleChange() {
259259
this.setState({value: this.refs.textarea.value});
260260
}
261-
// 將使用者輸入的 Markdown 語法 parse 成 HTML 放入 DOM 中,React 通常使用 virtual DOM 作為和 DOM 溝通的中介,不建議直接由操作 DOM。故使用時的屬性為 dangerouslySetInnerHTML
261+
// 將使用者輸入的 Markdown 語法 parse 成 HTML 放入 DOM 中,React 通常使用 virtual DOM 作為和 DOM 溝通的中介,不建議直接操作 DOM。故使用時的屬性為 dangerouslySetInnerHTML
262262
rawMarkup() {
263263
const md = new Remarkable();
264-
return { __html: md.render(this.state.value) };
264+
return { __html: md.render(this.state.value) };
265265
}
266266
render() {
267267
return (
@@ -277,7 +277,7 @@ class MarkdownEditor extends React.Component {
277277
dangerouslySetInnerHTML={this.rawMarkup()}
278278
/>
279279
</div>
280-
);
280+
);
281281
}
282282
}
283283

Ch04/react-component-life-cycle.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
// Prop 預設值,若對應 props 沒傳入值將會使用 default 值,為每個實例化 Component 共用的值
2828
MyComponent.defaultProps = {
29-
name: '',
29+
name: '',
3030
}
3131

3232
// 將 <MyComponent /> 元件插入 id 為 app 的 DOM 元素中
@@ -48,9 +48,9 @@
4848

4949
// Prop 預設值,若對應 props 沒傳入值將會使用 default 值
5050
MyComponent.defaultProps = {
51-
name: '',
51+
name: '',
5252
}
53-
53+
5454
// 將 <MyComponent /> 元件插入 id 為 app 的 DOM 元素中
5555
ReactDOM.render(<MyComponent name="Mark"/>, document.getElmentById('app'));
5656
```
@@ -77,7 +77,7 @@ React Component,就像人會有生老病死一樣有生命週期。一般而
7777
3. Unmounting
7878
- componentWillUnmount()
7979

80-
很多讀者一開始學習 Component 生命週期時會覺得很抽象,所以接下來用一個簡單範例讓大家感受一下 Component 的生命週期。讀者可以發現當一開始載入元件時第一個會觸發 `console.log('constructor');`,依序執行 `componentWillMount``componentDidMount` ,而當點擊文字觸發 `handleClick()` 更新 `state` 時則會依序執行 `componentWillUpdate``componentDidUpdate`
80+
很多讀者一開始學習 Component 生命週期時會覺得很抽象,所以接下來用一個簡單範例讓大家感受一下 Component 的生命週期。讀者可以發現當一開始載入元件時第一個會觸發 `console.log('constructor');`,依序執行 `componentWillMount``componentDidMount` ,而當點擊文字觸發 `handleClick()` 更新 `state` 時則會依序執行 `componentWillUpdate``componentDidUpdate`
8181

8282
HTML Markup:
8383
```html

0 commit comments

Comments
 (0)