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/forwarding-refs.md
+32-36Lines changed: 32 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -4,73 +4,69 @@ title: Forwarding Refs
4
4
permalink: docs/forwarding-refs.html
5
5
---
6
6
7
-
Ref forwarding is a technique for automatically passing a [ref](/docs/refs-and-the-dom.html)through a component to one of its children. This is typically not necessary for most components in the application. However, it can be useful for some kinds of components, especially in reusable component libraries. The most common scenarios are described below.
7
+
*Ref forwarding* adalah sebuah teknik untuk meneruskan [ref](/docs/refs-and-the-dom.html)secara otomatis melalui komponen ke salah satu anaknya. Ini biasanya tidak diperlukan untuk sebagian besar komponen dalam aplikasi. Namun, ini bisa berguna untuk beberapa jenis komponen, terutama di pustaka komponen yang dapat digunakan kembali. Skenario paling umum dijelaskan di bawah ini.
8
8
9
-
## Forwarding refs to DOM components {#forwarding-refs-to-dom-components}
9
+
## Mengoper refs ke komponen DOM {#forwarding-refs-to-dom-components}
10
10
11
-
Consider a `FancyButton`component that renders the native `button` DOM element:
11
+
Pertimbangkan sebuah komponen `FancyButton`yang me-render native DOM element yaitu `button`:
12
12
`embed:forwarding-refs/fancy-button-simple.js`
13
13
14
-
React components hide their implementation details, including their rendered output. Other components using `FancyButton`**usually will not need to**[obtain a ref](/docs/refs-and-the-dom.html)to the inner `button` DOM element. This is good because it prevents components from relying on each other's DOM structure too much.
14
+
Komponen React menyembunyikan detail implementasi mereka, termasuk keluaran render mereka. Komponen-komponen lainnya yang menggunakan `FancyButton`**biasanya tidak perlu untuk**[mendapatkan sebuah ref](/docs/refs-and-the-dom.html)ke dalam DOM element `button`. Ini bagus karena hal ini mencegah komponen untuk bersandar pada struktur DOM komponen lain yang berlebihan.
15
15
16
-
Although such encapsulation is desirable for application-level components like `FeedStory`or`Comment`, it can be inconvenient for highly reusable "leaf" components like `FancyButton`or`MyTextInput`. These components tend to be used throughout the application in a similar manner as a regular DOM `button`and`input`, and accessing their DOM nodes may be unavoidable for managing focus, selection, or animations.
16
+
Meskipun enkapsulasi seperti itu diperlukan oleh komponen yang berada di application-level seperti `FeedStory`atau`Comment`, namun hal tersebut dapat menyulitkan komponen-komponen "leaf" yang sering digunakan kembali seperti `FancyButton`atau`MyTextInput`. Komponen ini cenderung digunakan di seluruh aplikasi dengan cara yang sama seperti regular DOM `button`dan`input`, dan mungkin tidak dapat dihindari mengakses DOM nodes mereka untuk mengelola focus, selection, atau animation.
17
17
18
-
**Ref forwarding is an opt-in feature that lets some components take a `ref` they receive, and pass it further down (in other words, "forward" it) to a child.**
19
-
20
-
In the example below, `FancyButton` uses `React.forwardRef` to obtain the `ref` passed to it, and then forward it to the DOM `button` that it renders:
18
+
**Ref forwarding adalah sebuah fitur keikutsertaan yang memperbolehkan beberapa komponen-komponen mengambil sebuah `ref` yang didapatkan, dan menurunkannya (dengan kata lain, "meneruskan" nya) kepada child.**
21
19
20
+
Pada contoh di bawah ini, `FancyButton` menggunakan `React.forwardRef` untuk mendapatkan `ref` yang diteruskan kepadanya, lalu meneruskannya ke `button` DOM yang di render:
This way, components using `FancyButton`can get a ref to the underlying`button`DOM node and access it if necessary—just like if they used a DOM `button`directly.
23
+
Dengan cara ini, komponen-komponen yang menggunakan `FancyButton`bisa mendapatkan ref ke DOM node`button`yang mendasarinya dan mengaksesnya jika perlu - sama seperti jika mereka menggunakan `button`DOM secara langsung.
25
24
26
-
Here is a step-by-step explanation of what happens in the above example:
25
+
Berikut adalah penjelasan langkah demi langkah tentang apa yang terjadi pada contoh di atas:
27
26
28
-
1.We create a[React ref](/docs/refs-and-the-dom.html)by calling`React.createRef`and assign it to a `ref` variable.
29
-
1.We pass our `ref`down to`<FancyButton ref={ref}>`by specifying it as a JSX attribute.
30
-
1. React passes the `ref`to the`(props, ref) => ...`function inside`forwardRef`as a second argument.
31
-
1.We forward this`ref`argument down to `<button ref={ref}>`by specifying it as a JSX attribute.
32
-
1.When the ref is attached, `ref.current`will point to the `<button>` DOM node.
27
+
1.Kita buat sebuah[React ref](/docs/refs-and-the-dom.html)dengan memanggil`React.createRef`dan masukan kedalam variabel `ref`.
28
+
1.Kita teruskan `ref`tersebut ke`<FancyButton ref={ref}>`dengan menulisnya sebagai atribut JSX.
1.Kita teruskan argumen`ref`tersebut ke `<button ref={ref}>`dengan menulisnya sebagai atribut JSX.
31
+
1.Jika ref sudah terpasang, `ref.current`akan mengarah ke DOM node `<button>`.
33
32
34
-
>Note
33
+
>Catatan
35
34
>
36
-
>The second `ref`argument only exists when you define a component with `React.forwardRef` call. Regular function or class components don't receive the`ref`argument, and ref is not available in props either.
35
+
>Argumen `ref`kedua hanya ada saat Anda mendefinisikan komponen dengan `React.forwardRef`. Regular function atau class components tidak menerima argumen`ref`tersebut, dan ref juga tidak teredia di props.
37
36
>
38
-
>Ref forwarding is not limited to DOM components. You can forward refs to class component instances, too.
37
+
>Ref forwarding tidak terbatas pada komponen DOM. Anda juga dapat meneruskan refs ke class component instances.
39
38
40
-
## Note for component library maintainers {#note-for-component-library-maintainers}
39
+
## Catatan untuk pengelola pustaka komponen {#note-for-component-library-maintainers}
41
40
42
-
**When you start using`forwardRef`in a component library, you should treat it as a breaking change and release a new major version of your library.**This is because your library likely has an observably different behavior (such as what refs get assigned to, and what types are exported), and this can break apps and other libraries that depend on the old behavior.
41
+
**Saat Anda mulai menggunakan`forwardRef`di pustaka komponen, Anda harus memperlakukannya sebagai sebuah *breaking change* dan merilis versi mayor baru pustaka Anda.**Ini karena pustaka Anda kemungkinan besar memiliki perilaku yang sangat berbeda (seperti *refs* apa yang ditetapkan, dan tipe apa yang diekspor), dan ini bisa merusak aplikasi dan pustaka lain yang bergantung pada perilaku lama.
43
42
44
-
Conditionally applying `React.forwardRef`when it exists is also not recommended for the same reasons: it changes how your library behaves and can break your users' apps when they upgrade React itself.
43
+
Menerapkan `React.forwardRef`secara kondisional jika ada juga tidak disarankan karena alasan yang sama: ini mengubah cara pustaka Anda berperilaku dan bisa merusak aplikasi pengguna Anda saat mereka meningkatkan versi React itu sendiri.
45
44
46
-
## Forwarding refs in higher-order components {#forwarding-refs-in-higher-order-components}
45
+
## Mengoper refs dalam higher-order components {#forwarding-refs-in-higher-order-components}
47
46
48
-
This technique can also be particularly useful with[higher-order components](/docs/higher-order-components.html) (also known as HOCs). Let's start with an example HOC that logs component props to the console:
47
+
Teknik ini juga bisa menjadi sangat berguna untuk[higher-order components](/docs/higher-order-components.html) (biasa disebut dengan HOCs). Mari kita mulai dengan contoh HOC yang mengeluarkan component props ke log konsol:
49
48
`embed:forwarding-refs/log-props-before.js`
50
49
51
-
The "logProps" HOC passes all`props`through to the component it wraps, so the rendered output will be the same. For example, we can use this HOC to log all props that get passed to our "fancy button" component:
50
+
HOC "logProps" meneruskan semua`props`ke komponen yang dibungkusnya, sehingga keluaran yang dirender akan sama. Misalnya, kita dapat menggunakan HOC ini untuk mengeluarkan semua props ke log konsol yang diteruskan ke "fancy button" komponen kita:
52
51
`embed:forwarding-refs/fancy-button.js`
53
52
54
-
There is one caveat to the above example: refs will not get passed through. That's because`ref`is not a prop. Like`key`, it's handled differently by React. If you add a ref to a HOC, the ref will refer to the outermost container component, not the wrapped component.
53
+
Ada satu peringatan untuk contoh diatas: refs tidak akan diteruskan. Itu karena`ref`bukan sebuah prop. Seperti`key`, ini ditangani secara berbeda oleh React. Jika Anda menambahkan ref ke HOC, maka ref akan merujuk ke komponen container terluar, bukan komponen yang dibungkus.
55
54
56
-
This means that refs intended for our `FancyButton`component will actually be attached to the `LogProps`component:
55
+
Ini berarti bahwa ref yang dimaksudkan untuk komponen `FancyButton`kita sebenarnya akan dilampirkan ke `LogProps`komponen:
57
56
`embed:forwarding-refs/fancy-button-ref.js`
58
57
59
-
Fortunately, we can explicitly forward refs to the inner `FancyButton`component using the `React.forwardRef` API. `React.forwardRef`accepts a render function that receives`props`and`ref`parameters and returns a React node. For example:
58
+
Untungnya, kita dapat secara eksplisit meneruskan refs kedalam komponen `FancyButton`menggunakan `React.forwardRef` API. `React.forwardRef`menerima sebuah fungsi render yang menerima parameter`props`dan`ref`dan mengembalikan React node. Sebagai contoh:
60
59
`embed:forwarding-refs/log-props-after.js`
61
60
62
-
## Displaying a custom name in DevTools {#displaying-a-custom-name-in-devtools}
63
-
64
-
`React.forwardRef` accepts a render function. React DevTools uses this function to determine what to display for the ref forwarding component.
61
+
## Menampilkan nama custom di DevTools {#displaying-a-custom-name-in-devtools}
65
62
66
-
For example, the following component will appear as "*ForwardRef*" in the DevTools:
63
+
`React.forwardRef` menerima fungsi *render*. React DevTools menggunakan fungsi ini untuk menentukan apa yang akan ditampilkan untuk komponen *ref forwarding*.
67
64
65
+
Sebagai contoh, komponen berikut akan muncul sebagai "*ForwardRef*" di DevTools:
68
66
`embed:forwarding-refs/wrapped-component.js`
69
67
70
-
If you name the render function, DevTools will also include its name (e.g. "*ForwardRef(myFunction)*"):
71
-
68
+
Jika Anda menamai fungsi *render*, DevTools juga akan menyertakan namanya (misalnya "*ForwardRef(myFunction)*"):
0 commit comments