Skip to content

Commit cadaec8

Browse files
committed
modify
1 parent b253a07 commit cadaec8

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

content/docs/handling-events.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ React 元素的事件处理和 DOM 元素的很相似,但是有一点语法上
2121
</button>
2222
```
2323

24-
在 React 中有些略微不同
24+
在 React 中略微不同
2525

2626
```js{1}
2727
<button onClick={activateLasers}>
2828
Activate Lasers
2929
</button>
3030
```
3131

32-
在 React 中另一个不同点是你不能通过返回 `false` 的方式阻止默认行为。你必须显式的使用 preventDefault。例如,传统的 HTML 中阻止链接默认打开一个新页面,你可以这样写:
32+
在 React 中另一个不同点是你不能通过返回 `false` 的方式阻止默认行为。你必须显式的使用 `preventDefault` 。例如,传统的 HTML 中阻止链接默认打开一个新页面,你可以这样写:
3333

3434
```html
3535
<a href="#" onclick="console.log('The link was clicked.'); return false">
@@ -95,7 +95,7 @@ ReactDOM.render(
9595

9696
你必须谨慎对待 JSX 回调函数中的 `this`,在 JavaScript 中,class 的方法默认不会[绑定](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind) `this`。如果你忘记绑定 `this.handleClick` 并把它传入了 `onClick`,当你调用这个函数的时候 `this` 的值为 `undefined`
9797

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`
9999

100100
如果觉得使用 `bind` 很麻烦,这里有两种方式可以解决。如果你正在使用实验性的 [public class fields 语法](https://babeljs.io/docs/plugins/transform-class-properties/),你可以使用 class fileds 正确的绑定回调函数:
101101

@@ -117,9 +117,9 @@ class LoggingButton extends React.Component {
117117
}
118118
```
119119

120-
这个语法在 [Create React App](https://github.com/facebookincubator/create-react-app) 中默认开启
120+
[Create React App](https://github.com/facebookincubator/create-react-app) 默认启用此语法
121121

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)
123123

124124
```js{7-9}
125125
class LoggingButton extends React.Component {
@@ -140,7 +140,7 @@ class LoggingButton extends React.Component {
140140

141141
此语法问题在于每次渲染 `LoggingButton` 时都会创建不同的回调函数。在大多数情况下,这没什么问题,但如果该回调函数作为 prop 传入子组件时,这些组件可能会进行额外的重新渲染。我们通常建议在构造器中绑定或使用 class fileds 语法来避免这类性能问题。
142142

143-
## 向事件处理函数传递参数 {#passing-arguments-to-event-handlers}
143+
## 向事件处理程序传递参数 {#passing-arguments-to-event-handlers}
144144

145145
在循环中,通常我们会为事件处理函数传递额外的参数。例如,若 `id` 是你要删除那一行的 ID,以下两种方式都可以向事件处理函数传递参数:
146146

@@ -149,6 +149,6 @@ class LoggingButton extends React.Component {
149149
<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>
150150
```
151151

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) 来实现
153153

154154
在这两种情况下,参数 `e` 会作为 React 的事件对象被作为第二个参数传递。通过箭头函数的方式,事件对象必须显式的进行传递,但是通过 `bind` 的方式,事件对象以及更多的参数将会被隐式的进行传递。

0 commit comments

Comments
 (0)