From 2cc53a072e24c81c91e6d781954731f66835f8e8 Mon Sep 17 00:00:00 2001 From: Ryanhui Date: Sat, 9 Feb 2019 19:45:09 +0800 Subject: [PATCH 1/8] translate handling-events --- content/docs/handling-events.md | 52 ++++++++++++++++----------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/content/docs/handling-events.md b/content/docs/handling-events.md index 1f93772cea..5cf3358646 100644 --- a/content/docs/handling-events.md +++ b/content/docs/handling-events.md @@ -1,6 +1,6 @@ --- id: handling-events -title: Handling Events +title: 事件处理 permalink: docs/handling-events.html prev: state-and-lifecycle.html next: conditional-rendering.html @@ -8,12 +8,12 @@ redirect_from: - "docs/events-ko-KR.html" --- -Handling events with React elements is very similar to handling events on DOM elements. There are some syntactic differences: +React 元素的事件处理和 DOM元素的很相似,但是有一点语法上的不同: -* React events are named using camelCase, rather than lowercase. -* With JSX you pass a function as the event handler, rather than a string. +* React事件的命名采用驼峰式,而不是小写。 +* 采用 JSX 的语法你需要传入一个函数作为事件处理函数,而不是一个字符串。 -For example, the HTML: +例如,传统的 HTML: ```html ``` -is slightly different in React: +在React 中有些略微不同: ```js{1} ``` -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: +在 React 中另一个不同是你不能使用返回 false 的方式阻止默认行为。你必须明确的使用 preventDefault。例如,传统的 HTML 中阻止链接默认打开一个新页面,你可以这样写: ```html @@ -37,7 +37,7 @@ Another difference is that you cannot return `false` to prevent default behavior ``` -In React, this could instead be: +在 React,应该这样来写: ```js{2-5,8} function ActionLink() { @@ -54,11 +54,11 @@ function ActionLink() { } ``` -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. +在这里, `e` 是一个合成事件。React 根据 [W3C spec](https://www.w3.org/TR/DOM-Level-3-Events/) 来定义这些合成事件,所以你不需要担心跨浏览器的兼容性问题。 如果想了解更多,请查看 [`SyntheticEvent`](/docs/events.html) 参考指南。 -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. +使用 React 时你一般不需要使用 `addEventListener` 为一个已创建的 DOM 元素添加监听器。你仅仅需要在这个元素初始渲染的时候提供一个监听器。 -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: +当你使用 [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes) 语法定义一个组件的时候,通常的做法是使事件处理器成为类中的一个方法。例如,下面的 `Toggle` 组件渲染一个让用户切换开关状态的按钮: ```js{6,7,10-14,18} class Toggle extends React.Component { @@ -66,7 +66,7 @@ class Toggle extends React.Component { super(props); this.state = {isToggleOn: true}; - // This binding is necessary to make `this` work in the callback + // 为了在回调中使用 `this`,这个绑定是必不可少的 this.handleClick = this.handleClick.bind(this); } @@ -91,18 +91,18 @@ ReactDOM.render( ); ``` -[**Try it on CodePen**](http://codepen.io/gaearon/pen/xEmzGg?editors=0010) +[**在 CodePen 上试试**](http://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. +你必须谨慎对待 JSX 回调函数中的 `this`, 在 JavaScript 中,类的方法默认是不会[绑定](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind) `this` 的。如果你忘记绑定 `this.handleClick` 并把它传入了 `onClick`, 当你调用这个函数的时候 `this` 会是 `undefined`。 -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. +这并不是 React 的特殊行为;它是[函数如何在 JavaScript 中运行](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/)的一部分。通常情况下,如果你没有在方法后面添加 `()`,例如 `onClick={this.handleClick}`,你应该为这个方法绑定 `this`。 -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: +如果觉得使用 `bind` 很麻烦,这里有两种方式可以解决。 如果你正在使用实验性的[公有类字段语法](https://babeljs.io/docs/plugins/transform-class-properties/),你可以使用类字段正确的绑定回调函数: ```js{2-6} class LoggingButton extends React.Component { - // This syntax ensures `this` is bound within handleClick. - // Warning: this is *experimental* syntax. + // 这种语法确保 `this` 绑定在 `handleClick` 内。 + // 注意: 这是 *实验性* 语法。 handleClick = () => { console.log('this is:', this); } @@ -117,9 +117,9 @@ class LoggingButton extends React.Component { } ``` -This syntax is enabled by default in [Create React App](https://github.com/facebookincubator/create-react-app). +这个语法在 [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: +如果你没有使用类字段语法,你可以在回调函数中使用[箭头函数](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions): ```js{7-9} class LoggingButton extends React.Component { @@ -128,7 +128,7 @@ class LoggingButton extends React.Component { } render() { - // This syntax ensures `this` is bound within handleClick + // 这种语法确保 `this` 绑定在 `handleClick` 内。 return ( ``` -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. +上述两种方式是等价的,分别通过 [arrow functions](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) 来为事件处理函数传递参数。 -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. +上面两个例子中,参数 `e` 作为 React 事件对象将会被作为第二个参数进行传递。通过箭头函数的方式,事件对象必须显式的进行传递,但是通过 `bind` 的方式,事件对象以及更多的参数将会被隐式的进行传递。 \ No newline at end of file From 9e217c883815f23d353c9bc31d77cc3734d7a8aa Mon Sep 17 00:00:00 2001 From: Ryanhui Date: Wed, 13 Feb 2019 16:57:08 +0800 Subject: [PATCH 2/8] modify handling-events.md --- content/docs/handling-events.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/content/docs/handling-events.md b/content/docs/handling-events.md index 5cf3358646..ac56dc6191 100644 --- a/content/docs/handling-events.md +++ b/content/docs/handling-events.md @@ -8,9 +8,9 @@ redirect_from: - "docs/events-ko-KR.html" --- -React 元素的事件处理和 DOM元素的很相似,但是有一点语法上的不同: +React 元素的事件处理和 DOM 元素的很相似,但是有一点语法上的不同: -* React事件的命名采用驼峰式,而不是小写。 +* React 事件的命名采用驼峰式,而不是小写。 * 采用 JSX 的语法你需要传入一个函数作为事件处理函数,而不是一个字符串。 例如,传统的 HTML: @@ -21,7 +21,7 @@ React 元素的事件处理和 DOM元素的很相似,但是有一点语法上 ``` -在React 中有些略微不同: +在 React 中有些略微不同: ```js{1} ``` -在 React 中另一个不同是你不能使用返回 false 的方式阻止默认行为。你必须明确的使用 preventDefault。例如,传统的 HTML 中阻止链接默认打开一个新页面,你可以这样写: +在 React 中另一个不同是你不能使用返回 `false` 的方式阻止默认行为。你必须显式的使用 preventDefault。例如,传统的 HTML 中阻止链接默认打开一个新页面,你可以这样写: ```html @@ -54,11 +54,11 @@ function ActionLink() { } ``` -在这里, `e` 是一个合成事件。React 根据 [W3C spec](https://www.w3.org/TR/DOM-Level-3-Events/) 来定义这些合成事件,所以你不需要担心跨浏览器的兼容性问题。 如果想了解更多,请查看 [`SyntheticEvent`](/docs/events.html) 参考指南。 +在这里,`e` 是一个合成事件。React 根据 [W3C spec](https://www.w3.org/TR/DOM-Level-3-Events/) 来定义这些合成事件,所以你不需要担心跨浏览器的兼容性问题。 如果想了解更多,请查看 [`SyntheticEvent`](/docs/events.html) 参考指南。 使用 React 时你一般不需要使用 `addEventListener` 为一个已创建的 DOM 元素添加监听器。你仅仅需要在这个元素初始渲染的时候提供一个监听器。 -当你使用 [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes) 语法定义一个组件的时候,通常的做法是使事件处理器成为类中的一个方法。例如,下面的 `Toggle` 组件渲染一个让用户切换开关状态的按钮: +当你使用 [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes) 语法定义一个组件的时候,通常的做法是使事件处理函数成为类中的一个方法。例如,下面的 `Toggle` 组件渲染一个让用户切换开关状态的按钮: ```js{6,7,10-14,18} class Toggle extends React.Component { @@ -140,9 +140,9 @@ class LoggingButton extends React.Component { 使用这个语法有个问题就是每次 `LoggingButton` 渲染的时候都会创建一个不同的回调函数。在大多数情况下,这没有问题。然而如果这个回调函数作为一个属性值传入低阶组件,这些组件可能会进行额外的重新渲染。我们通常建议在构造器中绑定或使用类字段语法来避免这类性能问题。 -## 向事件处理程序传递参数 +## 向事件处理函数传递参数 -在循环中,通常我们会为事件处理程序传递额外的参数。例如,若 `id` 是你要删除那一行的 ID,以下两种方式都可以向事件处理程序传递参数: +在循环中,通常我们会为事件处理函数传递额外的参数。例如,若 `id` 是你要删除那一行的 ID,以下两种方式都可以向事件处理函数传递参数: ```js From 1add2e63a3f2206b9718ead2f733e8b828c1cb8c Mon Sep 17 00:00:00 2001 From: Ryanhui Date: Tue, 19 Feb 2019 16:57:25 +0800 Subject: [PATCH 3/8] modify --- content/docs/handling-events.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/handling-events.md b/content/docs/handling-events.md index ac56dc6191..d389bb2d8b 100644 --- a/content/docs/handling-events.md +++ b/content/docs/handling-events.md @@ -11,7 +11,7 @@ redirect_from: React 元素的事件处理和 DOM 元素的很相似,但是有一点语法上的不同: * React 事件的命名采用驼峰式,而不是小写。 -* 采用 JSX 的语法你需要传入一个函数作为事件处理函数,而不是一个字符串。 +* 采用 JSX 的语法时你需要传入一个函数作为事件处理函数,而不是一个字符串。 例如,传统的 HTML: From 1e547bd9d1aa8ff1afdd1d178592700a3d8a5e80 Mon Sep 17 00:00:00 2001 From: Ryanhui Date: Tue, 19 Feb 2019 17:09:48 +0800 Subject: [PATCH 4/8] not translate class --- content/docs/handling-events.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/content/docs/handling-events.md b/content/docs/handling-events.md index d389bb2d8b..d69d33d62e 100644 --- a/content/docs/handling-events.md +++ b/content/docs/handling-events.md @@ -29,7 +29,7 @@ React 元素的事件处理和 DOM 元素的很相似,但是有一点语法上 ``` -在 React 中另一个不同是你不能使用返回 `false` 的方式阻止默认行为。你必须显式的使用 preventDefault。例如,传统的 HTML 中阻止链接默认打开一个新页面,你可以这样写: +在 React 中另一个不同是你不能使用返回 `false` 的方式阻止默认行为。你必须显式的使用 preventDefault 。例如,传统的 HTML 中阻止链接默认打开一个新页面,你可以这样写: ```html @@ -58,7 +58,7 @@ function ActionLink() { 使用 React 时你一般不需要使用 `addEventListener` 为一个已创建的 DOM 元素添加监听器。你仅仅需要在这个元素初始渲染的时候提供一个监听器。 -当你使用 [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes) 语法定义一个组件的时候,通常的做法是使事件处理函数成为类中的一个方法。例如,下面的 `Toggle` 组件渲染一个让用户切换开关状态的按钮: +当你使用 [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes) 语法定义一个组件的时候,通常的做法是使事件处理函数成为 class 中的一个方法。例如,下面的 `Toggle` 组件渲染一个让用户切换开关状态的按钮: ```js{6,7,10-14,18} class Toggle extends React.Component { @@ -93,7 +93,7 @@ ReactDOM.render( [**在 CodePen 上试试**](http://codepen.io/gaearon/pen/xEmzGg?editors=0010) -你必须谨慎对待 JSX 回调函数中的 `this`, 在 JavaScript 中,类的方法默认是不会[绑定](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind) `this` 的。如果你忘记绑定 `this.handleClick` 并把它传入了 `onClick`, 当你调用这个函数的时候 `this` 会是 `undefined`。 +你必须谨慎对待 JSX 回调函数中的 `this`, 在 JavaScript 中, class 的方法默认是不会[绑定](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind) `this` 的。如果你忘记绑定 `this.handleClick` 并把它传入了 `onClick`,当你调用这个函数的时候 `this` 会是 `undefined`。 这并不是 React 的特殊行为;它是[函数如何在 JavaScript 中运行](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/)的一部分。通常情况下,如果你没有在方法后面添加 `()`,例如 `onClick={this.handleClick}`,你应该为这个方法绑定 `this`。 @@ -119,7 +119,7 @@ class LoggingButton extends React.Component { 这个语法在 [Create React App](https://github.com/facebookincubator/create-react-app) 中默认开启。 -如果你没有使用类字段语法,你可以在回调函数中使用[箭头函数](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions): +如果你没有使用 class 字段语法,你可以在回调函数中使用[箭头函数](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions): ```js{7-9} class LoggingButton extends React.Component { From 425b56b56ef24ea2e14c8bc4d2b266929ed2ad89 Mon Sep 17 00:00:00 2001 From: Ryanhui Date: Tue, 26 Feb 2019 18:01:35 +0800 Subject: [PATCH 5/8] modify --- content/docs/handling-events.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/content/docs/handling-events.md b/content/docs/handling-events.md index d69d33d62e..b04f658ac0 100644 --- a/content/docs/handling-events.md +++ b/content/docs/handling-events.md @@ -10,8 +10,8 @@ redirect_from: React 元素的事件处理和 DOM 元素的很相似,但是有一点语法上的不同: -* React 事件的命名采用驼峰式,而不是小写。 -* 采用 JSX 的语法时你需要传入一个函数作为事件处理函数,而不是一个字符串。 +* React 事件的命名采用小驼峰式(camelCase),而不是纯小写。 +* 使用 JSX 语法时你需要传入一个函数作为事件处理函数,而不是一个字符串。 例如,传统的 HTML: @@ -29,7 +29,7 @@ React 元素的事件处理和 DOM 元素的很相似,但是有一点语法上 ``` -在 React 中另一个不同是你不能使用返回 `false` 的方式阻止默认行为。你必须显式的使用 preventDefault 。例如,传统的 HTML 中阻止链接默认打开一个新页面,你可以这样写: +在 React 中另一个不同点是你不能通过返回 `false` 的方式阻止默认行为。你必须显式的使用 preventDefault。例如,传统的 HTML 中阻止链接默认打开一个新页面,你可以这样写: ```html @@ -37,7 +37,7 @@ React 元素的事件处理和 DOM 元素的很相似,但是有一点语法上 ``` -在 React,应该这样来写: +在 React 中,可能是这样的: ```js{2-5,8} function ActionLink() { @@ -54,11 +54,11 @@ function ActionLink() { } ``` -在这里,`e` 是一个合成事件。React 根据 [W3C spec](https://www.w3.org/TR/DOM-Level-3-Events/) 来定义这些合成事件,所以你不需要担心跨浏览器的兼容性问题。 如果想了解更多,请查看 [`SyntheticEvent`](/docs/events.html) 参考指南。 +在这里,`e` 是一个合成事件。React 根据 [W3C 规范](https://www.w3.org/TR/DOM-Level-3-Events/)来定义这些合成事件,所以你不需要担心跨浏览器的兼容性问题。如果想了解更多,请查看 [`SyntheticEvent`](/docs/events.html) 参考指南。 -使用 React 时你一般不需要使用 `addEventListener` 为一个已创建的 DOM 元素添加监听器。你仅仅需要在这个元素初始渲染的时候提供一个监听器。 +使用 React 时,你一般不需要使用 `addEventListener` 为已创建的 DOM 元素添加监听器。React恰恰与之相反,你仅需要在该元素初始渲染的时候添加一个监听器。 -当你使用 [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes) 语法定义一个组件的时候,通常的做法是使事件处理函数成为 class 中的一个方法。例如,下面的 `Toggle` 组件渲染一个让用户切换开关状态的按钮: +当你使用 [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes) 语法定义一个组件的时候,通常的做法是将事件处理函数声明为 class 中的方法。例如,下面的 `Toggle` 组件会渲染一个让用户切换开关状态的按钮: ```js{6,7,10-14,18} class Toggle extends React.Component { @@ -91,13 +91,13 @@ ReactDOM.render( ); ``` -[**在 CodePen 上试试**](http://codepen.io/gaearon/pen/xEmzGg?editors=0010) +[**在 CodePen 上尝试**](http://codepen.io/gaearon/pen/xEmzGg?editors=0010) -你必须谨慎对待 JSX 回调函数中的 `this`, 在 JavaScript 中, class 的方法默认是不会[绑定](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind) `this` 的。如果你忘记绑定 `this.handleClick` 并把它传入了 `onClick`,当你调用这个函数的时候 `this` 会是 `undefined`。 +你必须谨慎对待 JSX 回调函数中的 `this`,在 JavaScript 中,class 的方法默认不会[绑定](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind) `this`。如果你忘记绑定 `this.handleClick` 并把它传入了 `onClick`,当你调用这个函数的时候 `this` 的值为 `undefined`。 -这并不是 React 的特殊行为;它是[函数如何在 JavaScript 中运行](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/)的一部分。通常情况下,如果你没有在方法后面添加 `()`,例如 `onClick={this.handleClick}`,你应该为这个方法绑定 `this`。 +这并不是 React 的特殊行为;这其实与[函数在 JavaScript 中工作原理](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/)有关。通常情况下,如果你没有在方法后面添加 `()`,例如 `onClick={this.handleClick}`,你应该为这个方法绑定 `this`。 -如果觉得使用 `bind` 很麻烦,这里有两种方式可以解决。 如果你正在使用实验性的[公有类字段语法](https://babeljs.io/docs/plugins/transform-class-properties/),你可以使用类字段正确的绑定回调函数: +如果觉得使用 `bind` 很麻烦,这里有两种方式可以解决。如果你正在使用实验性的 [public class fields 语法](https://babeljs.io/docs/plugins/transform-class-properties/),你可以使用 class fileds 正确的绑定回调函数: ```js{2-6} class LoggingButton extends React.Component { @@ -119,7 +119,7 @@ class LoggingButton extends React.Component { 这个语法在 [Create React App](https://github.com/facebookincubator/create-react-app) 中默认开启。 -如果你没有使用 class 字段语法,你可以在回调函数中使用[箭头函数](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions): +如果你没有使用 class fileds 语法,你可以在回调函数中使用[箭头函数](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions): ```js{7-9} class LoggingButton extends React.Component { @@ -138,7 +138,7 @@ class LoggingButton extends React.Component { } ``` -使用这个语法有个问题就是每次 `LoggingButton` 渲染的时候都会创建一个不同的回调函数。在大多数情况下,这没有问题。然而如果这个回调函数作为一个属性值传入低阶组件,这些组件可能会进行额外的重新渲染。我们通常建议在构造器中绑定或使用类字段语法来避免这类性能问题。 +此语法问题在于每次渲染 `LoggingButton` 时都会创建不同的回调函数。在大多数情况下,这没什么问题,但如果该回调函数作为 prop 传入子组件时,这些组件可能会进行额外的重新渲染。我们通常建议在构造器中绑定或使用 class fileds 语法来避免这类性能问题。 ## 向事件处理函数传递参数 @@ -149,6 +149,6 @@ class LoggingButton extends React.Component { ``` -上述两种方式是等价的,分别通过 [arrow functions](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) 来为事件处理函数传递参数。 +上述两种方式是等价的,分别通过[箭头函数](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) 来为事件处理函数传递参数。 -上面两个例子中,参数 `e` 作为 React 事件对象将会被作为第二个参数进行传递。通过箭头函数的方式,事件对象必须显式的进行传递,但是通过 `bind` 的方式,事件对象以及更多的参数将会被隐式的进行传递。 \ No newline at end of file +在这两种情况下,参数 `e` 会作为 React 的事件对象被作为第二个参数传递。通过箭头函数的方式,事件对象必须显式的进行传递,但是通过 `bind` 的方式,事件对象以及更多的参数将会被隐式的进行传递。 \ No newline at end of file From 9bf8a1aa4bec1e71903d2b2c4e54916b2f7b422f Mon Sep 17 00:00:00 2001 From: QiChang Li Date: Tue, 26 Feb 2019 21:47:45 +0800 Subject: [PATCH 6/8] fix: code pen --- content/docs/handling-events.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/docs/handling-events.md b/content/docs/handling-events.md index ab97d844a0..d0eecb25c0 100644 --- a/content/docs/handling-events.md +++ b/content/docs/handling-events.md @@ -91,7 +91,7 @@ ReactDOM.render( ); ``` -[**在 CodePen 上尝试**](http://codepen.io/gaearon/pen/xEmzGg?editors=0010) +[**在 CodePen 上尝试**](https://codepen.io/gaearon/pen/xEmzGg?editors=0010) 你必须谨慎对待 JSX 回调函数中的 `this`,在 JavaScript 中,class 的方法默认不会[绑定](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind) `this`。如果你忘记绑定 `this.handleClick` 并把它传入了 `onClick`,当你调用这个函数的时候 `this` 的值为 `undefined`。 @@ -151,4 +151,4 @@ class LoggingButton extends React.Component { 上述两种方式是等价的,分别通过[箭头函数](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) 来为事件处理函数传递参数。 -在这两种情况下,参数 `e` 会作为 React 的事件对象被作为第二个参数传递。通过箭头函数的方式,事件对象必须显式的进行传递,但是通过 `bind` 的方式,事件对象以及更多的参数将会被隐式的进行传递。 \ No newline at end of file +在这两种情况下,参数 `e` 会作为 React 的事件对象被作为第二个参数传递。通过箭头函数的方式,事件对象必须显式的进行传递,但是通过 `bind` 的方式,事件对象以及更多的参数将会被隐式的进行传递。 From cadaec8d65a3dfc745e1acde686f848c8ea12ba8 Mon Sep 17 00:00:00 2001 From: Ryanhui Date: Wed, 27 Feb 2019 21:55:09 +0800 Subject: [PATCH 7/8] modify --- content/docs/handling-events.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/content/docs/handling-events.md b/content/docs/handling-events.md index ab97d844a0..dc9ee8b657 100644 --- a/content/docs/handling-events.md +++ b/content/docs/handling-events.md @@ -21,7 +21,7 @@ React 元素的事件处理和 DOM 元素的很相似,但是有一点语法上 ``` -在 React 中有些略微不同: +在 React 中略微不同: ```js{1} ``` -在 React 中另一个不同点是你不能通过返回 `false` 的方式阻止默认行为。你必须显式的使用 preventDefault。例如,传统的 HTML 中阻止链接默认打开一个新页面,你可以这样写: +在 React 中另一个不同点是你不能通过返回 `false` 的方式阻止默认行为。你必须显式的使用 `preventDefault` 。例如,传统的 HTML 中阻止链接默认打开一个新页面,你可以这样写: ```html @@ -95,7 +95,7 @@ ReactDOM.render( 你必须谨慎对待 JSX 回调函数中的 `this`,在 JavaScript 中,class 的方法默认不会[绑定](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind) `this`。如果你忘记绑定 `this.handleClick` 并把它传入了 `onClick`,当你调用这个函数的时候 `this` 的值为 `undefined`。 -这并不是 React 的特殊行为;这其实与[函数在 JavaScript 中工作原理](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/)有关。通常情况下,如果你没有在方法后面添加 `()`,例如 `onClick={this.handleClick}`,你应该为这个方法绑定 `this`。 +这并不是 React 特有的行为;这其实与 [JavaScript 函数工作原理](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/)有关。通常情况下,如果你没有在方法后面添加 `()`,例如 `onClick={this.handleClick}`,你应该为这个方法绑定 `this`。 如果觉得使用 `bind` 很麻烦,这里有两种方式可以解决。如果你正在使用实验性的 [public class fields 语法](https://babeljs.io/docs/plugins/transform-class-properties/),你可以使用 class fileds 正确的绑定回调函数: @@ -117,9 +117,9 @@ class LoggingButton extends React.Component { } ``` -这个语法在 [Create React App](https://github.com/facebookincubator/create-react-app) 中默认开启。 +[Create React App](https://github.com/facebookincubator/create-react-app) 默认启用此语法。 -如果你没有使用 class fileds 语法,你可以在回调函数中使用[箭头函数](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions): +如果你没有使用 class fileds 语法,你可以在回调中使用[箭头函数](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions): ```js{7-9} class LoggingButton extends React.Component { @@ -140,7 +140,7 @@ class LoggingButton extends React.Component { 此语法问题在于每次渲染 `LoggingButton` 时都会创建不同的回调函数。在大多数情况下,这没什么问题,但如果该回调函数作为 prop 传入子组件时,这些组件可能会进行额外的重新渲染。我们通常建议在构造器中绑定或使用 class fileds 语法来避免这类性能问题。 -## 向事件处理函数传递参数 {#passing-arguments-to-event-handlers} +## 向事件处理程序传递参数 {#passing-arguments-to-event-handlers} 在循环中,通常我们会为事件处理函数传递额外的参数。例如,若 `id` 是你要删除那一行的 ID,以下两种方式都可以向事件处理函数传递参数: @@ -149,6 +149,6 @@ class LoggingButton extends React.Component { ``` -上述两种方式是等价的,分别通过[箭头函数](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) 来为事件处理函数传递参数。 +上述两种方式是等价的,分别通过[箭头函数](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) 来实现。 在这两种情况下,参数 `e` 会作为 React 的事件对象被作为第二个参数传递。通过箭头函数的方式,事件对象必须显式的进行传递,但是通过 `bind` 的方式,事件对象以及更多的参数将会被隐式的进行传递。 \ No newline at end of file From 3f312e80f250bcee33f9f5d4b617ec49cc46f9d7 Mon Sep 17 00:00:00 2001 From: Ryanhui Date: Thu, 28 Feb 2019 10:56:56 +0800 Subject: [PATCH 8/8] modify --- content/docs/handling-events.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/content/docs/handling-events.md b/content/docs/handling-events.md index 375f815c2e..d6b02b95c8 100644 --- a/content/docs/handling-events.md +++ b/content/docs/handling-events.md @@ -151,4 +151,5 @@ class LoggingButton extends React.Component { 上述两种方式是等价的,分别通过[箭头函数](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) 来实现。 -在这两种情况下,参数 `e` 会作为 React 的事件对象被作为第二个参数传递。通过箭头函数的方式,事件对象必须显式的进行传递,但是通过 `bind` 的方式,事件对象以及更多的参数将会被隐式的进行传递。 +在这两种情况下,React 的事件对象 `e` 会被作为第二个参数传递。如果通过箭头函数的方式,事件对象必须显式的进行传递,而通过 `bind` 的方式,事件对象以及更多的参数将会被隐式的进行传递。 +