diff --git a/content/docs/forwarding-refs.md b/content/docs/forwarding-refs.md index 3318d8499..6808c0b50 100644 --- a/content/docs/forwarding-refs.md +++ b/content/docs/forwarding-refs.md @@ -1,76 +1,75 @@ --- id: forwarding-refs -title: Forwarding Refs +title: 傳送 Ref permalink: docs/forwarding-refs.html --- -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. +傳送 ref 是一種自動把 [ref](/docs/refs-and-the-dom.html) 從一個 component 傳遞到它底下的其中一個 child 的技巧。通常來說,應用程式裡大部分的 component 都不需要用到它。然而,對某些種類的 component 來說它很有用,特別是能夠重複使用的函式庫。以下會解釋最常見的情形。 -## Forwarding refs to DOM components {#forwarding-refs-to-dom-components} +## 傳送 ref 到 DOM component {#forwarding-refs-to-dom-components} -Consider a `FancyButton` component that renders the native `button` DOM element: -`embed:forwarding-refs/fancy-button-simple.js` +試著考慮一個叫做 `FancyButton` 的 component,它會 render 一個原生的 `button` DOM element:`embed:forwarding-refs/fancy-button-simple.js` -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. +React component 會隱藏包含 render 的結果在內的實作細節。其他使用到 `FancyButton` 的 component **通常不需要**獲得內部按鈕的 DOM element 的 [ref](/docs/refs-and-the-dom.html)。這樣帶來的好處是,我們能夠避免讓其他 component 過度依賴彼此的 DOM 的結構。 -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. +雖然這樣的封裝對於應用程式層級的 component(像是 `FeedStory` 或 `Comment`)來說是我們所希望擁有的,但對於常用的「末端」 component,像是 `FancyButton` 或 `MyTextInput` 來說,可能會變得不方便使用。這些末端 component 通常會像普通的 DOM `button` 和 `input` 一樣,在應用程式的各個地方被使用,在處理 focus、選取或動畫時,取得它們的 DOM 節點可能是不可避免的。 -**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.** +**傳送 ref 是個選擇性的功能,它能夠讓某些 component 利用它們收到的 `ref` 來傳遞到底下的 child component。** -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: +在下面的例子中,`FancyButton` 藉由 `React.forwardRef` 來獲取傳遞到它身上的 `ref`,然後再傳遞到它 render 的 DOM `button` 上: `embed:forwarding-refs/fancy-button-simple-ref.js` -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. +這樣一來,使用 `FancyButton` 的 component 可以獲得它底下的 `button` 的 DOM 節點的 ref,並可以在需要的時候獲取它 —— 就如同直接使用 `button` DOM 一樣。 -Here is a step-by-step explanation of what happens in the above example: +以下一步一步的解釋上面的例子到底發生了什麼事: -1. We create a [React ref](/docs/refs-and-the-dom.html) by calling `React.createRef` and assign it to a `ref` variable. -1. We pass our `ref` down to `` by specifying it as a JSX attribute. -1. React passes the `ref` to the `(props, ref) => ...` function inside `forwardRef` as a second argument. -1. We forward this `ref` argument down to `