@@ -32,7 +32,7 @@ app.js,使用 ES6 Class Component 寫法:
32
32
class HelloMessage extends React .Component {
33
33
// 若是需要綁定 this.方法或是需要在 constructor 使用 props,定義 state,就需要 constructor。若是在其他方法(如 render)使用 this.props 則不用一定要定義 constructor
34
34
constructor (props ) {
35
- // 對於 OOP 物件導向程式設計熟悉的讀者應該對於 constructor 建構子的使用不陌生,事實上它是 ES6 的語法糖,骨子裡還是 portotype based 物件導向程式語言。透過 extends 可以繼承 React.Component 父類別。super 方法可以呼叫繼承父類別的建構子
35
+ // 對於 OOP 物件導向程式設計熟悉的讀者應該對於 constructor 建構子的使用不陌生,事實上它是 ES6 的語法糖,骨子裡還是 prototype based 物件導向程式語言。透過 extends 可以繼承 React.Component 父類別。super 方法可以呼叫繼承父類別的建構子
36
36
super (props);
37
37
this .state = {}
38
38
}
@@ -51,7 +51,7 @@ HelloMessage.propTypes = {
51
51
52
52
// Prop 預設值,若對應 props 沒傳入值將會使用 default 值 Zuck
53
53
HelloMessage .defaultProps = {
54
- name: ' Zuck' ,
54
+ name: ' Zuck' ,
55
55
}
56
56
57
57
ReactDOM .render (< HelloMessage name= " Mark" / > , document .getElementById (' app' ));
@@ -62,7 +62,7 @@ ReactDOM.render(<HelloMessage name="Mark" />, document.getElementById('app'));
62
62
使用 Functional Component 寫法:
63
63
64
64
``` javascript
65
- // Functional Component 可以視為 f(d) => UI,根據傳進去的 props 繪出對應的 UI。注意這邊 props 是傳入函式的參數。 因此取用 props 不用加 this
65
+ // Functional Component 可以視為 f(d) => UI,根據傳進去的 props 繪出對應的 UI。注意這邊 props 是傳入函式的參數, 因此取用 props 不用加 this
66
66
const HelloMessage = (props ) => (
67
67
< div> Hello {props .name }< / div>
68
68
);
@@ -74,7 +74,7 @@ HelloMessage.propTypes = {
74
74
75
75
// Prop 預設值,若對應 props 沒傳入值將會使用 default 值 Zuck。用法等於 ES5 的 getDefaultProps
76
76
HelloMessage .defaultProps = {
77
- name: ' Zuck' ,
77
+ name: ' Zuck' ,
78
78
}
79
79
80
80
ReactDOM .render (< HelloMessage name= " Mark" / > , document .getElementById (' app' ));
@@ -112,7 +112,7 @@ app.js:
112
112
class Timer extends React .Component {
113
113
constructor (props ) {
114
114
super (props);
115
- // 與 ES5 React.createClass({}) 不同的是 component 內自定義的方法需要自行綁定 this context,或是使用 arrow function
115
+ // 與 ES5 React.createClass({}) 不同的是 component 內自定義的方法需要自行綁定 this context,或是使用 arrow function
116
116
this .tick = this .tick .bind (this );
117
117
// 初始 state,等於 ES5 中的 getInitialState
118
118
this .state = {
@@ -127,15 +127,15 @@ class Timer extends React.Component {
127
127
componentDidMount () {
128
128
this .interval = setInterval (this .tick , 1000 );
129
129
}
130
- // componentWillUnmount 為 component 生命週期中 component 即將移出插入的節點的階段。這邊移除了 setInterval 效力
130
+ // componentWillUnmount 為 component 生命週期中 component 即將移出插入的節點的階段。這邊移除了 setInterval 效力
131
131
componentWillUnmount () {
132
132
clearInterval (this .interval );
133
133
}
134
134
// render 為 class Component 中唯一需要定義的方法,其回傳 component 欲顯示的內容
135
135
render () {
136
136
return (
137
137
< div> Seconds Elapsed: {this .state .secondsElapsed }< / div>
138
- );
138
+ );
139
139
}
140
140
}
141
141
@@ -192,7 +192,7 @@ class TodoApp extends React.Component {
192
192
}
193
193
}
194
194
onChange (e ) {
195
- this .setState ({text: e .target .value });
195
+ this .setState ({text: e .target .value });
196
196
}
197
197
handleSubmit (e ) {
198
198
e .preventDefault ();
@@ -258,10 +258,10 @@ class MarkdownEditor extends React.Component {
258
258
handleChange () {
259
259
this .setState ({value: this .refs .textarea .value });
260
260
}
261
- // 將使用者輸入的 Markdown 語法 parse 成 HTML 放入 DOM 中,React 通常使用 virtual DOM 作為和 DOM 溝通的中介,不建議直接由操作 DOM。故使用時的屬性為 dangerouslySetInnerHTML
261
+ // 將使用者輸入的 Markdown 語法 parse 成 HTML 放入 DOM 中,React 通常使用 virtual DOM 作為和 DOM 溝通的中介,不建議直接操作 DOM。故使用時的屬性為 dangerouslySetInnerHTML
262
262
rawMarkup () {
263
263
const md = new Remarkable ();
264
- return { __html: md .render (this .state .value ) };
264
+ return { __html: md .render (this .state .value ) };
265
265
}
266
266
render () {
267
267
return (
@@ -277,7 +277,7 @@ class MarkdownEditor extends React.Component {
277
277
dangerouslySetInnerHTML= {this .rawMarkup ()}
278
278
/ >
279
279
< / div>
280
- );
280
+ );
281
281
}
282
282
}
283
283
0 commit comments