From 08cc694025076d05e5fca76f85cfcfdc1edd86b2 Mon Sep 17 00:00:00 2001 From: Muhammad Irwan Andriawan Date: Mon, 8 May 2023 03:27:49 +0700 Subject: [PATCH] Translate Manipulating the DOM with Refs page (#366) --- .../learn/manipulating-the-dom-with-refs.md | 186 +++++++++--------- 1 file changed, 93 insertions(+), 93 deletions(-) diff --git a/src/content/learn/manipulating-the-dom-with-refs.md b/src/content/learn/manipulating-the-dom-with-refs.md index b5c193763..fa0a550e0 100644 --- a/src/content/learn/manipulating-the-dom-with-refs.md +++ b/src/content/learn/manipulating-the-dom-with-refs.md @@ -1,52 +1,52 @@ --- -title: 'Manipulating the DOM with Refs' +title: 'Memanipulasi DOM dengan Refs' --- -React automatically updates the [DOM](https://developer.mozilla.org/docs/Web/API/Document_Object_Model/Introduction) to match your render output, so your components won't often need to manipulate it. However, sometimes you might need access to the DOM elements managed by React--for example, to focus a node, scroll to it, or measure its size and position. There is no built-in way to do those things in React, so you will need a *ref* to the DOM node. +React secara otomatis memperbarui [DOM (*Document Object Model*)](https://developer.mozilla.org/docs/Web/API/Document_Object_Model/Introduction) agar sesuai dengan keluaran *render*, sehingga komponen Anda tidak perlu sering memanipulasinya. Namun, terkadang Anda mungkin perlu mengakses elemen DOM yang dikelola oleh React--misalnya, untuk memberikan fokus pada sebuah simpul (*node*), menggulir ke sana, atau mengukur ukuran dan posisinya. Tidak ada cara bawaan untuk melakukan hal-hal tersebut di React, sehingga Anda memerlukan *ref* ke simpul DOM. -- How to access a DOM node managed by React with the `ref` attribute -- How the `ref` JSX attribute relates to the `useRef` Hook -- How to access another component's DOM node -- In which cases it's safe to modify the DOM managed by React +- Bagaimana cara mengakses sebuah simpul DOM yang diatur React dengan atribut `ref` +- Bagaimana atribut JSX `ref` berelasi dengan Hook `useRef` +- Bagaimana cara mengakses simpul DOM dari komponen lain +- Dalam kasus seperti apa memodifikasi DOM yang diatur React dengan aman. -## Getting a ref to the node {/*getting-a-ref-to-the-node*/} +## Mendapatkan sebuah ref untuk simpul {/*getting-a-ref-to-the-node*/} -To access a DOM node managed by React, first, import the `useRef` Hook: +Untuk mengakses sebuah simpul DOM yang diatur React, pertama, impor Hook `useRef`: ```js import { useRef } from 'react'; ``` -Then, use it to declare a ref inside your component: +Kemudian, deklarasikan *ref* di dalam komponen Anda: ```js const myRef = useRef(null); ``` -Finally, pass it to the DOM node as the `ref` attribute: +Terakhir, oper ke simpul DOM sebagai atribut `ref`: ```js
``` -The `useRef` Hook returns an object with a single property called `current`. Initially, `myRef.current` will be `null`. When React creates a DOM node for this `
`, React will put a reference to this node into `myRef.current`. You can then access this DOM node from your [event handlers](/learn/responding-to-events) and use the built-in [browser APIs](https://developer.mozilla.org/docs/Web/API/Element) defined on it. +Hook `useRef` mengembalikan objek dengan properti tunggal bernama `current`. Awalnya, `myRef.current` akan bernilai `null`. Saat React membuat simpul DOM untuk `
`, React akan mereferensikan simpul ini ke dalam `myRef.current`. Anda bisa mengakses simpul DOM ini dari [*event handlers*](/learn/responding-to-events) Anda dan menggunakan [API peramban](https://developer.mozilla.org/docs/Web/API/Element) bawaan. ```js -// You can use any browser APIs, for example: +// Anda dapat menggunakan API peramban apa saja, sebagai contoh: myRef.current.scrollIntoView(); ``` -### Example: Focusing a text input {/*example-focusing-a-text-input*/} +### Contoh: Fokus ke sebuah input teks {/*example-focusing-a-text-input*/} -In this example, clicking the button will focus the input: +Pada contoh ini, meng-klik tombol tersebut akan fokus ke input: @@ -73,18 +73,18 @@ export default function Form() { -To implement this: +Penerapannya: -1. Declare `inputRef` with the `useRef` Hook. -2. Pass it as ``. This tells React to **put this ``'s DOM node into `inputRef.current`.** -3. In the `handleClick` function, read the input DOM node from `inputRef.current` and call [`focus()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus) on it with `inputRef.current.focus()`. -4. Pass the `handleClick` event handler to `