You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: content/docs/handling-events.md
+27-26
Original file line number
Diff line number
Diff line change
@@ -1,43 +1,43 @@
1
1
---
2
2
id: handling-events
3
-
title: Handling Events
3
+
title: 事件处理
4
4
permalink: docs/handling-events.html
5
5
prev: state-and-lifecycle.html
6
6
next: conditional-rendering.html
7
7
redirect_from:
8
8
- "docs/events-ko-KR.html"
9
9
---
10
10
11
-
Handling events with React elements is very similar to handling events on DOM elements. There are some syntactic differences:
11
+
React 元素的事件处理和 DOM 元素的很相似,但是有一点语法上的不同:
12
12
13
-
* React events are named using camelCase, rather than lowercase.
14
-
*With JSX you pass a function as the event handler, rather than a string.
13
+
* React 事件的命名采用小驼峰式(camelCase),而不是纯小写。
14
+
*使用 JSX 语法时你需要传入一个函数作为事件处理函数,而不是一个字符串。
15
15
16
-
For example, the HTML:
16
+
例如,传统的 HTML:
17
17
18
18
```html
19
19
<buttononclick="activateLasers()">
20
20
Activate Lasers
21
21
</button>
22
22
```
23
23
24
-
is slightly different in 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
-
Another difference is that you cannot return `false`to prevent default behavior in React. You must call `preventDefault`explicitly. For example, with plain HTML, to prevent the default link behavior of opening a new page, you can write:
32
+
在 React 中另一个不同点是你不能通过返回 `false`的方式阻止默认行为。你必须显式的使用 `preventDefault`。例如,传统的 HTML 中阻止链接默认打开一个新页面,你可以这样写:
33
33
34
34
```html
35
35
<ahref="#"onclick="console.log('The link was clicked.'); return false">
36
36
Click me
37
37
</a>
38
38
```
39
39
40
-
In React, this could instead be:
40
+
在 React 中,可能是这样的:
41
41
42
42
```js{2-5,8}
43
43
function ActionLink() {
@@ -54,19 +54,19 @@ function ActionLink() {
54
54
}
55
55
```
56
56
57
-
Here, `e`is a synthetic event. React defines these synthetic events according to the [W3C spec](https://www.w3.org/TR/DOM-Level-3-Events/), so you don't need to worry about cross-browser compatibility. See the [`SyntheticEvent`](/docs/events.html)reference guide to learn more.
When using React you should generally not need to call `addEventListener`to add listeners to a DOM element after it is created. Instead, just provide a listener when the element is initially rendered.
59
+
使用 React 时,你一般不需要使用 `addEventListener`为已创建的 DOM 元素添加监听器。React恰恰与之相反,你仅需要在该元素初始渲染的时候添加一个监听器。
60
60
61
-
When you define a component using an [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes), a common pattern is for an event handler to be a method on the class. For example, this `Toggle`component renders a button that lets the user toggle between "ON" and "OFF" states:
61
+
当你使用 [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes) 语法定义一个组件的时候,通常的做法是将事件处理函数声明为 class 中的方法。例如,下面的 `Toggle`组件会渲染一个让用户切换开关状态的按钮:
62
62
63
63
```js{6,7,10-14,18}
64
64
class Toggle extends React.Component {
65
65
constructor(props) {
66
66
super(props);
67
67
this.state = {isToggleOn: true};
68
68
69
-
// This binding is necessary to make `this` work in the callback
69
+
// 为了在回调中使用 `this`,这个绑定是必不可少的
70
70
this.handleClick = this.handleClick.bind(this);
71
71
}
72
72
@@ -91,18 +91,18 @@ ReactDOM.render(
91
91
);
92
92
```
93
93
94
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/xEmzGg?editors=0010)
You have to be careful about the meaning of `this` in JSX callbacks. In JavaScript, class methods are not [bound](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind)by default. If you forget to bind `this.handleClick`and pass it to `onClick`,`this`will be `undefined` when the function is actually called.
This is not React-specific behavior; it is a part of [how functions work in JavaScript](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/). Generally, if you refer to a method without `()` after it, such as `onClick={this.handleClick}`, you should bind that method.
If calling `bind`annoys you, there are two ways you can get around this. If you are using the experimental [public class fields syntax](https://babeljs.io/docs/plugins/transform-class-properties/), you can use class fields to correctly bind callbacks:
100
+
如果觉得使用 `bind`很麻烦,这里有两种方式可以解决。如果你正在使用实验性的 [public class fields 语法](https://babeljs.io/docs/plugins/transform-class-properties/),你可以使用 class fileds 正确的绑定回调函数:
101
101
102
102
```js{2-6}
103
103
class LoggingButton extends React.Component {
104
-
// This syntax ensures `this` is bound within handleClick.
105
-
// Warning: this is *experimental* syntax.
104
+
// 这种语法确保 `this` 绑定在 `handleClick` 内。
105
+
// 注意: 这是 *实验性* 语法。
106
106
handleClick = () => {
107
107
console.log('this is:', this);
108
108
}
@@ -117,9 +117,9 @@ class LoggingButton extends React.Component {
117
117
}
118
118
```
119
119
120
-
This syntax is enabled by default in [Create React App](https://github.com/facebookincubator/create-react-app).
If you aren't using class fields syntax, you can use an [arrow function](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions) in the callback:
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 {
@@ -128,7 +128,7 @@ class LoggingButton extends React.Component {
128
128
}
129
129
130
130
render() {
131
-
// This syntax ensures `this` is bound within handleClick
131
+
// 这种语法确保 `this` 绑定在 `handleClick` 内。
132
132
return (
133
133
<button onClick={(e) => this.handleClick(e)}>
134
134
Click me
@@ -138,17 +138,18 @@ class LoggingButton extends React.Component {
138
138
}
139
139
```
140
140
141
-
The problem with this syntax is that a different callback is created each time the `LoggingButton`renders. In most cases, this is fine. However, if this callback is passed as a prop to lower components, those components might do an extra re-rendering. We generally recommend binding in the constructor or using the class fields syntax, to avoid this sort of performance problem.
141
+
此语法问题在于每次渲染 `LoggingButton`时都会创建不同的回调函数。在大多数情况下,这没什么问题,但如果该回调函数作为 prop 传入子组件时,这些组件可能会进行额外的重新渲染。我们通常建议在构造器中绑定或使用 class fileds 语法来避免这类性能问题。
142
142
143
-
## Passing Arguments to Event Handlers {#passing-arguments-to-event-handlers}
Inside a loop it is common to want to pass an extra parameter to an event handler. For example, if `id`is the row ID, either of the following would work:
The above two lines are equivalent, and use [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) and [`Function.prototype.bind`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind) respectively.
In both cases, the `e` argument representing the React event will be passed as a second argument after the ID. With an arrow function, we have to pass it explicitly, but with `bind` any further arguments are automatically forwarded.
0 commit comments