Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

Translates/handling events #13

Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions content/docs/handling-events.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
---
id: handling-events
title: Handling Events
title: การจัดการเหตุการณ์
permalink: docs/handling-events.html
prev: state-and-lifecycle.html
next: conditional-rendering.html
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 elements นั้นมีความคล้ายคลึงกับการจัดการเหตุการณ์บน DOM elements มาก มีเพียงไวยากรณ์ที่ต่างกันเล็กน้อย

* React events are named using camelCase, rather than lowercase.
* With JSX you pass a function as the event handler, rather than a string.
* เหตุการณ์ที่เกิดบน React ใช้วิธีเขียนชื่อแบบ camelCase แทนที่จะเป็นภาษาอังกฤษตัวพิมพ์เล็ก
* เราส่งฟังก์ชั่นเข้ามาเป็นตัวจัดการเหตุการณ์ใน JSX เลย แทนที่จะเป็น string

For example, the HTML:
ยกตัวอย่างเช่น ในกรณีที่เป็น HTML:

```html
<button onclick="activateLasers()">
Activate Lasers
</button>
```

is slightly different in React:
จะเห็นว่าแตกต่างจาก React เพียงเล็กน้อย

```js{1}
<button onClick={activateLasers}>
Activate Lasers
</button>
```

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
<a href="#" onclick="console.log('The link was clicked.'); return false">
Click me
</a>
```

In React, this could instead be:
ส่วนใน React นั้นจะเขียนแบบนี้

```js{2-5,8}
function ActionLink() {
Expand All @@ -54,19 +54,19 @@ 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](https://www.w3.org/TR/DOM-Level-3-Events/) ดังนั้นจึงไม่ต้องกังวลกับการรองรับการใช้งานในหลายบราว์เซอร์ ดูรายละเอียดเพิ่มเติมได้ที่[`เหตุการณ์สังเคราะห์`](/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 element หลังจากที่มันถูกสร้าง เราเพียงแค่ให้ข้อมูลการรับฟัง(listener) เมื่อ element นั้นถูกนำไปแสดงผลในครั้งแรก

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](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes) ตัวจัดการเหตุการณ์โดยทั่วไปนั้นคือ method ของคลาส ยกตัวอย่างเช่นคอมโพเนนท์ `Toggle` ที่มีการแสดงสถานะ "เปิด" (ON) และ "ปิด" (OFF) โดยผู้ใช้สามารถสลับค่าไปมาได้

```js{6,7,10-14,18}
class Toggle extends React.Component {
constructor(props) {
super(props);
this.state = {isToggleOn: true};

// This binding is necessary to make `this` work in the callback
// การ bind นี้เป็นสิ่งที่จำเป็นและทำให้ `this` ใช้งานได้ใน callback
this.handleClick = this.handleClick.bind(this);
}

Expand All @@ -91,18 +91,18 @@ ReactDOM.render(
);
```

[**Try it on CodePen**](https://codepen.io/gaearon/pen/xEmzGg?editors=0010)
[**ทดลองเขียนบน 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` ที่มากับ callback ของ JSX โดยในคลาส JavaScript นั้น method จะยังไม่ได้ถูก [bind](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind) มาให้ ดังนั้นถ้าเราลืม bind `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 แต่มันเป็น[วิธีการทำงานของฟังก์ชั่น JavaScritp](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/) ทั่วไป นั้นคือโดยทั่วไปแล้วถ้าเราอ้างถึงฟังก์ชั่นโดยที่ไม่มี `()` ต่อท้ายเช่น `onClick={this.handleClick}` เราควร bind 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:
ถ้าการเรียก `bind` นั้นดูไม่น่าใช้ ยังมีอีกสองวิธีที่สามารถทำได้ โดยใช้ [ไวยากรณ์ class fields](https://babeljs.io/docs/plugins/transform-class-properties/) ที่ยังอยู่ในช่วงทดลองนั้นจัดการ bind ให้กับ callback

```js{2-6}
class LoggingButton extends React.Component {
// This syntax ensures `this` is bound within handleClick.
// Warning: this is *experimental* syntax.
// ไวยากรณ์แบบนี้จะให้ทำให้ `this` นั้นถูก bind ภายใน handleClick
// ระวัง: ไวยากรณ์แบบนี้ยังอยู่ในช่วง *ทดลอง*
handleClick = () => {
console.log('this is:', this);
}
Expand All @@ -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:
แต่ถ้าไม่ได้ใช้ไวยากรณ์ class fields เราก็สามารถใช้[ฟังก์ชั่นแบบลูกศร](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions) ใน callback ได้

```js{7-9}
class LoggingButton extends React.Component {
Expand All @@ -128,7 +128,7 @@ class LoggingButton extends React.Component {
}

render() {
// This syntax ensures `this` is bound within handleClick
// ไวยากรณ์แบบนี้จะให้ทำให้ `this` นั้นถูก bind ภายใน handleClick
return (
<button onClick={(e) => this.handleClick(e)}>
Click me
Expand All @@ -138,17 +138,17 @@ class LoggingButton extends React.Component {
}
```

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.
ปัญหาของการเขียนแบบนี้คือจะมีการสร้าง callback ใหม่ทุกครั้งที่มีการแสดงผล `LogginButton` ซึ่งโดยทั่วไปแล้วสามารถทำได้ แต่ถ้า callback นี้ถูกส่งเข้าเป็น prop ของคอมโพเนนท์ที่อยู่ภายในอีก อาจจะส่งผลให้คอมโพเนนท์เหล่านั้นมีการสร้างการแสดงผลใหม่อย่างไม่จำเป็น เราจึงแนะนำให้ทำการ bind ที่ส่วน constructor หรือใช้ไวยากรณ์ class fields เพื่อหลีกเลี่ยงปัญหาด้านประสิทธิภาพ

## Passing Arguments to Event Handlers {#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:
ภายใน Loop เรามักจะต้องการพารามิเตอร์ที่เพิ่มขึ้นในการจัดการเหตุการณ์ ยกตัวอย่างเช่นถ้า `id` เป็นค่าเฉพาะของแต่ละแถว(ID)

```js
<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>
<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>
```

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.
การเขียนทั้งสองบรรทัดบนนั้นเหมือนกัน โดยใช้[ฟังก์ชั่นแบบลูกศร](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 โดยมันจะถูกส่งเข้าไปลำดับที่สองต่อจาก ID ถ้าเขียนแบบฟังก์ชั่นลูกศร เราต้องเขียน `e` ลงไปเองให้ชัดเจนเลย แต่ในแบบ `bind` ค่าของ `e` นั้นจะถูกส่งต่อเข้าไปเองโดยอัตโนมัต
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is my suggestion
อัตโนมัต -> อัตโนมัติ