@@ -21,15 +21,15 @@ React 元素的事件处理和 DOM 元素的很相似,但是有一点语法上
21
21
</button >
22
22
```
23
23
24
- 在 React 中有些略微不同 :
24
+ 在 React 中略微不同 :
25
25
26
26
``` js{1}
27
27
<button onClick={activateLasers}>
28
28
Activate Lasers
29
29
</button>
30
30
```
31
31
32
- 在 React 中另一个不同点是你不能通过返回 ` false ` 的方式阻止默认行为。你必须显式的使用 preventDefault。例如,传统的 HTML 中阻止链接默认打开一个新页面,你可以这样写:
32
+ 在 React 中另一个不同点是你不能通过返回 ` false ` 的方式阻止默认行为。你必须显式的使用 ` preventDefault ` 。例如,传统的 HTML 中阻止链接默认打开一个新页面,你可以这样写:
33
33
34
34
``` html
35
35
<a href =" #" onclick =" console.log('The link was clicked.'); return false" >
@@ -95,7 +95,7 @@ ReactDOM.render(
95
95
96
96
你必须谨慎对待 JSX 回调函数中的 ` this ` ,在 JavaScript 中,class 的方法默认不会[ 绑定] ( https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind ) ` this ` 。如果你忘记绑定 ` this.handleClick ` 并把它传入了 ` onClick ` ,当你调用这个函数的时候 ` this ` 的值为 ` undefined ` 。
97
97
98
- 这并不是 React 的特殊行为 ;这其实与[ 函数在 JavaScript 中工作原理 ] ( https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/ ) 有关。通常情况下,如果你没有在方法后面添加 ` () ` ,例如 ` onClick={this.handleClick} ` ,你应该为这个方法绑定 ` this ` 。
98
+ 这并不是 React 特有的行为 ;这其实与 [ JavaScript 函数工作原理 ] ( https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/ ) 有关。通常情况下,如果你没有在方法后面添加 ` () ` ,例如 ` onClick={this.handleClick} ` ,你应该为这个方法绑定 ` this ` 。
99
99
100
100
如果觉得使用 ` bind ` 很麻烦,这里有两种方式可以解决。如果你正在使用实验性的 [ public class fields 语法] ( https://babeljs.io/docs/plugins/transform-class-properties/ ) ,你可以使用 class fileds 正确的绑定回调函数:
101
101
@@ -117,9 +117,9 @@ class LoggingButton extends React.Component {
117
117
}
118
118
```
119
119
120
- 这个语法在 [ Create React App] ( https://github.com/facebookincubator/create-react-app ) 中默认开启 。
120
+ [ Create React App] ( https://github.com/facebookincubator/create-react-app ) 默认启用此语法 。
121
121
122
- 如果你没有使用 class fileds 语法,你可以在回调函数中使用 [ 箭头函数] ( https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions ) :
122
+ 如果你没有使用 class fileds 语法,你可以在回调中使用 [ 箭头函数] ( https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions ) :
123
123
124
124
``` js{7-9}
125
125
class LoggingButton extends React.Component {
@@ -140,7 +140,7 @@ class LoggingButton extends React.Component {
140
140
141
141
此语法问题在于每次渲染 ` LoggingButton ` 时都会创建不同的回调函数。在大多数情况下,这没什么问题,但如果该回调函数作为 prop 传入子组件时,这些组件可能会进行额外的重新渲染。我们通常建议在构造器中绑定或使用 class fileds 语法来避免这类性能问题。
142
142
143
- ## 向事件处理函数传递参数 {#passing-arguments-to-event-handlers}
143
+ ## 向事件处理程序传递参数 {#passing-arguments-to-event-handlers}
144
144
145
145
在循环中,通常我们会为事件处理函数传递额外的参数。例如,若 ` id ` 是你要删除那一行的 ID,以下两种方式都可以向事件处理函数传递参数:
146
146
@@ -149,6 +149,6 @@ class LoggingButton extends React.Component {
149
149
< button onClick= {this .deleteRow .bind (this , id)}> Delete Row< / button>
150
150
```
151
151
152
- 上述两种方式是等价的,分别通过[ 箭头函数] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions ) 和 [ ` Function.prototype.bind ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind ) 来为事件处理函数传递参数 。
152
+ 上述两种方式是等价的,分别通过[ 箭头函数] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions ) 和 [ ` Function.prototype.bind ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind ) 来实现 。
153
153
154
154
在这两种情况下,参数 ` e ` 会作为 React 的事件对象被作为第二个参数传递。通过箭头函数的方式,事件对象必须显式的进行传递,但是通过 ` bind ` 的方式,事件对象以及更多的参数将会被隐式的进行传递。
0 commit comments