diff --git a/content/docs/handling-events.md b/content/docs/handling-events.md index 175303c24..7741cb629 100644 --- a/content/docs/handling-events.md +++ b/content/docs/handling-events.md @@ -1,6 +1,6 @@ --- id: handling-events -title: Handling Events +title: Penanganan Events permalink: docs/handling-events.html prev: state-and-lifecycle.html next: conditional-rendering.html @@ -8,28 +8,28 @@ 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: +Menangani *events* dengan elemen React sangat mirip seperti menangani sebuah *events* pada elemen DOM. Ada beberapa perbedaan sintaksis: -* React events are named using camelCase, rather than lowercase. -* With JSX you pass a function as the event handler, rather than a string. +* *Events* pada React biasanya ditulis dalam bentuk *camelCase*, bukan *lowercase*. +* Dengan JSX Anda dapat mengoper *function* sebagai *event handler*, bukan sebagai *string*. -For example, the HTML: +Sebagai contoh pada HTML berikut ini: ```html ``` -is slightly different in React: +sedikit berbeda dengan 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: +Perbedaan lainnya adalah Anda tidak dapat mengembalikan nilai `false` untuk mencegah *behavior* bawaan React. Anda harus memanggil `preventDefault` secara eksplisit. Sebagai contoh, pada HTML untuk mencegah agar *link* bawaan membuka halaman baru, Anda dapat menulis seperti ini: ```html @@ -37,28 +37,28 @@ Another difference is that you cannot return `false` to prevent default behavior ``` -In React, this could instead be: +Sedangkan pada React, contoh tersebut dapat ditulis sebagai berikut: ```js{2-5,8} function ActionLink() { function handleClick(e) { e.preventDefault(); - console.log('The link was clicked.'); + console.log('Tautan diklik.'); } return ( - Click me + Klik Saya ); } ``` -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. +Di sini, `e` adalah sebuah *event* tiruan. React mendefinisikan event tiruan ini berdasarkan [W3C spec](https://www.w3.org/TR/DOM-Level-3-Events/), jadi Anda tidak perlu khawatir akan kesesuaian antar lintas *browser*. Lihat referensi pada [`SyntheticEvent`](/docs/events.html) untuk panduan belajar lebih jauh. -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. +Ketika menggunakan React, pada umumnya Anda tidak perlu memanggil `addEventListener` untuk menambahkan *listener* pada elemen DOM setelah dibuat. Sebagai gantinya, cukup berikan *listener* ketika elemen pertama kali di-*render*. -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: +Ketika Anda mendefinisikan sebuah komponen dengan menggunakan [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes), pada umumnya penanganan *event* sebagai sebuah *method* dalam *class*. Sebagai contoh, komponen `Toggle` ini me-*render* sebuah tombol yang memungkinkan pengguna untuk mengubah kondisi "ON" dan "OFF" pada sebuah *state*: ```js{6,7,10-14,18} class Toggle extends React.Component { @@ -66,7 +66,8 @@ class Toggle extends React.Component { super(props); this.state = {isToggleOn: true}; - // This binding is necessary to make `this` work in the callback + // cara binding seperti ini diperlukan untuk membuat `this` dapat berfungsi - + // pada callback binding this.handleClick = this.handleClick.bind(this); } @@ -91,18 +92,18 @@ ReactDOM.render( ); ``` -[**Try it on CodePen**](https://codepen.io/gaearon/pen/xEmzGg?editors=0010) +[**Coba di 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. +Anda harus berhati-hati terhadap makna dari `this` pada JSX *callbacks*. Dalam JavaScript, *class method* tidak [terikat](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind) secara bawaan. Jika Anda lupa untuk melakukan *binding* `this.handleClick` dan mengoperkan pada `onClick`, maka `this` akan menjadi `undefined` ketika sebuah *function* telah dipanggil. -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. +Ini bukan *behavior* yang spesifik pada React, tetapi ini merupakan bagian dari [bagaimana *functions* dalam JavaScript bekerja](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/). Pada umumnya jika Anda mendefinisikan sebuah *method* tanpa diakhiri dengan `()`, seperti `onClick={this.handleClick}`, maka Anda harus melakukan *binding* terhadap *method* tersebut. -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: +Jika Anda tidak terbiasa menggunakan `bind`, ada dua cara untuk mengatasi ini. Jika Anda menggunakan sintaksis eksperimental [public class fields](https://babeljs.io/docs/plugins/transform-class-properties/), Anda dapat menggunakan *class fields* untuk melakukan *binding* terhadap *callbacks*: ```js{2-6} class LoggingButton extends React.Component { - // This syntax ensures `this` is bound within handleClick. - // Warning: this is *experimental* syntax. + // Sintaksis ini memastikan `this` telah terikat dalam handleClick. + // Peringatan: Ini adalah eksperimental sintaksis. handleClick = () => { console.log('this is:', this); } @@ -110,16 +111,16 @@ class LoggingButton extends React.Component { render() { return ( ); } } ``` -This syntax is enabled by default in [Create React App](https://github.com/facebookincubator/create-react-app). +Sintaksis ini telah diakfifkan secara bawaan pada [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: +Jika Anda tidak menggunakan sintaksis *class fields*, Anda dapat menggunakan [arrow function](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions) pada *callback*: ```js{7-9} class LoggingButton extends React.Component { @@ -128,27 +129,27 @@ class LoggingButton extends React.Component { } render() { - // This syntax ensures `this` is bound within handleClick + // Sintaksis ini memastikan `this` telah terikat dalam handleClick. return ( ); } } ``` -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. +Masalah pada sintaksis tersebut adalah *callback* yang berbeda dibuat setiap kali `LoggingButton` di-*render*. Dalam banyak kasus, hal ini tidak masalah. Akan tetapi, jika callback tersebut mengoperkan sebagai *props* kepada komponen yang lebih rendah, maka komponen tersebut mungkin akan melakukan ekstra *render* ulang. Kita umumnya merekomendasikan *binding* dilakukan pada *constructor* atau menggunakan sintaksis *class fields*, untuk menghindari masalah kinerja seperti ini. -## Passing Arguments to Event Handlers {#passing-arguments-to-event-handlers} +## Mengoper Argumen ke dalam Penanganan Event {#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: +Di dalam perulangan biasanya ingin mengoper sebuah parameter ekstra kedalam penanganan *event*. Sebagai contoh, jika `id` sama dengan baris ID, maka salah satu dari kedua dapat dijalankan: ```js ``` -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. +Dua baris di atas memiliki arti yang sama, masing-masing menggunakan [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) dan [`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. +Dalam kedua kasus tersebut, argumen `e` merepresentasikan *event* React yang akan dioper sebagai argumen kedua setelah ID. Dengan *arrow function*, kita harus mengeoperkan secara eksplisit, namun dengan `bind` segala argumen setelahnya akan diteruskan secara otomatis.