Skip to content

Commit fe6d624

Browse files
authored
Merge pull request #191 from huykon/translate/forwarding-refs
Translate document article "Forwarding Refs"
2 parents dd30748 + 82735a6 commit fe6d624

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

content/docs/forwarding-refs.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,76 @@
11
---
22
id: forwarding-refs
3-
title: Forwarding Refs
3+
title: Chuyển Tiếp Refs
44
permalink: docs/forwarding-refs.html
55
---
66

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+
Chuyển tiếp Refs là một kỹ thuật để tự động chuyển một [ref](/docs/refs-and-the-dom.html) qua một component đến một trong các component con của nó. Điều này thường không cần thiết đối với hầu hết các components trong ứng dụng. Tuy nhiên, nó có thể hữu ích cho một số loại component, đặc biệt là trong các thư viện component có thể tái sử dụng. Các tình huống phổ biến nhất được mô tả dưới đây.
88

9-
## Forwarding refs to DOM components {#forwarding-refs-to-dom-components}
9+
## Chuyển tiếp refs tới các DOM components {#forwarding-refs-to-dom-components}
1010

11-
Consider a `FancyButton` component that renders the native `button` DOM element:
11+
Hãy xem xét component `FancyButton` hiển thị phần tử DOM của `button` gốc:
1212
`embed:forwarding-refs/fancy-button-simple.js`
1313

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+
Các React components ẩn chi tiết triển khai của chúng, bao gồm cả đầu ra được hiển thị của chúng. Các components khác sử dụng `FancyButton` **thường sẽ không cần** [lấy tham chiếu](/docs/refs-and-the-dom.html) đến phần tử DOM của `button` bên trong. Điều này là tốt vì nó ngăn các components dựa vào cấu trúc DOM của nhau quá nhiều.
1515

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+
Mặc dù việc đóng gói như vậy là mong muốn đối với các components cấp ứng dụng như `FeedStory` hoặc `Comment`, nhưng nó có thể gây bất tiện cho các components “leaf” có thể tái sử dụng cao như `FancyButton` hoặc `MyTextInput`. Các components này có xu hướng được sử dụng trong toàn bộ ứng dụng theo cách tương tự như `button` `input` DOM thông thường, và việc truy cập vào các DOM nodes của chúng có thể không thể tránh khỏi để quản lý tiêu điểm, lựa chọn hoặc hoạt ảnh.
1717

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.**
18+
**Chuyển tiếp Ref là một tính năng chọn tham gia cho phép một số components nhận `tham chiếu` mà chúng nhận được và chuyển tiếp nó xuống (nói cách khác, “chuyển tiếp” nó) cho thành phần con.**
1919

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:
20+
Trong ví dụ dưới đây, `FancyButton` sử dụng `React.forwardRef` để lấy `ref` được chuyển đến nó, sau đó chuyển tiếp nó đến DOM `button` mà nó hiển thị:
2121

2222
`embed:forwarding-refs/fancy-button-simple-ref.js`
2323

24-
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.
24+
Bằng cách này, các components sử dụng `FancyButton` có thể nhận được tham chiếu đến DOM `button` bên dưới và truy cập nó nếu cần - giống như nếu chúng sử dụng trực tiếp DOM `button`.
2525

26-
Here is a step-by-step explanation of what happens in the above example:
26+
Dưới đây là giải thích từng bước về những gì xảy ra trong ví dụ trên:
2727

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.
28+
1. Chúng ta tạo một [React ref](/docs/refs-and-the-dom.html) bằng cách gọi `React.createRef` và gán nó cho một biến` ref`.
29+
1. Chúng ta chuyển `ref` xuống`<FancyButton ref={ref}>` bằng cách chỉ định nó như một thuộc tính JSX.
30+
1. React chuyển `ref` tới hàm `(props, ref) => ...` bên trong `forwardRef` như một đối số thứ hai.
31+
1. Chúng ta chuyển tiếp đối số `ref` này xuống `<button ref={ref}>` bằng cách chỉ định nó làm thuộc tính JSX.
32+
1. Khi ref được đính kèm, `ref.current` sẽ trỏ đến DOM node `<button>`.
3333

34-
>Note
34+
>Chú ý
3535
>
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.
36+
>Đối số `ref` thứ hai chỉ tồn tại khi bạn xác định một component với lệnh gọi `React.forwardRef`. Các function hoặc class component thông thường không nhận được đối số `ref` ref cũng không có sẵn trong các props.
3737
>
38-
>Ref forwarding is not limited to DOM components. You can forward refs to class component instances, too.
38+
>Chuyển tiếp Ref không giới hạn đối với các DOM components. Bạn cũng có thể chuyển tiếp các refs đến các class component cá thể.
3939
40-
## Note for component library maintainers {#note-for-component-library-maintainers}
40+
## Lưu ý cho người bảo trì thư viện component {#note-for-component-library-maintainers}
4141

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.
42+
**Khi bạn bắt đầu sử dụng `forwardRef` trong thư viện component, bạn nên coi nó như một thay đổi đột phá và phát hành một phiên bản chính mới của thư viện của bạn.** Điều này là do thư viện của bạn có thể có một hành vi khác nhau có thể quan sát được (chẳng hạn như những ref được gán cho, và những loại được xuất ra) và điều này có thể phá vỡ các ứng dụng và thư viện khác phụ thuộc vào hành vi cũ.
4343

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.
44+
Việc áp dụng có điều kiện `React.forwardRef` khi nó tồn tại cũng không được khuyến khích vì những lý do tương tự: nó thay đổi cách thư viện của bạn hoạt động và có thể phá vỡ ứng dụng của người dùng khi họ tự nâng cấp React.
4545

46-
## Forwarding refs in higher-order components {#forwarding-refs-in-higher-order-components}
46+
## Chuyển tiếp refs trong higher-order components {#forwarding-refs-in-higher-order-components}
4747

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:
48+
Kỹ thuật này cũng có thể đặc biệt hữu ích với [higher-order components](/docs/higher-order-components.html) (còn được gọi là HOCs). Hãy bắt đầu với một HOC mẫu logs ra các props vào console:
4949
`embed:forwarding-refs/log-props-before.js`
5050

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:
51+
HOC "logProps" chuyển tất cả các `props` đến component mà nó bao bọc, vì vậy kết quả hiển thị sẽ giống nhau. Ví dụ, chúng ta có thể sử dụng HOC này để log lại tất cả các props được chuyển đến component "fancy button":
5252
`embed:forwarding-refs/fancy-button.js`
5353

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.
54+
Có một lưu ý cho ví dụ trên: refs sẽ không được chuyển qua. Đó là bởi vì `ref` không phải là một prop. Giống như `key`, nó được React xử lý theo cách khác. Nếu bạn thêm một ref vào HOC, ref này sẽ tham chiếu đến thành phần chứa ngoài cùng, không phải thành phần được bao bọc.
5555

56-
This means that refs intended for our `FancyButton` component will actually be attached to the `LogProps` component:
56+
Điều này có nghĩa là các refs dành cho component `FancyButton` của chúng ta sẽ thực sự được gắn vào component `LogProps`:
5757
`embed:forwarding-refs/fancy-button-ref.js`
5858

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:
59+
May mắn thay, chúng ta có thể chuyển tiếp rõ ràng các refs đến thành phần `FancyButton` bên trong bằng cách sử dụng API `React.forwardRef`. `React.forwardRef` chấp nhận một hàm render nhận các tham số `props` `ref` và trả về một React node. Ví dụ:
6060
`embed:forwarding-refs/log-props-after.js`
6161

62-
## Displaying a custom name in DevTools {#displaying-a-custom-name-in-devtools}
62+
## Hiển thị tên tùy chỉnh trong DevTools {#displaying-a-custom-name-in-devtools}
6363

64-
`React.forwardRef` accepts a render function. React DevTools uses this function to determine what to display for the ref forwarding component.
64+
`React.forwardRef` chấp nhận một hàm render. React DevTools sử dụng chức năng này để xác định những gì sẽ hiển thị cho component chuyển tiếp ref.
6565

66-
For example, the following component will appear as "*ForwardRef*" in the DevTools:
66+
Ví dụ, component sau sẽ xuất hiện dưới dạng "*ForwardRef*" trong DevTools.
6767

6868
`embed:forwarding-refs/wrapped-component.js`
6969

70-
If you name the render function, DevTools will also include its name (e.g. "*ForwardRef(myFunction)*"):
70+
Nếu bạn đặt tên cho hàm render, DevTools cũng sẽ bao gồm tên của nó (ví dụ: "*ForwardRef(myFunction)*"):
7171

7272
`embed:forwarding-refs/wrapped-component-with-function-name.js`
7373

74-
You can even set the function's `displayName` property to include the component you're wrapping:
74+
Bạn thậm chí có thể đặt thuộc tính `displayName` của hàm để bao gồm thành phần mà bạn đang gói:
7575

7676
`embed:forwarding-refs/customized-display-name.js`

0 commit comments

Comments
 (0)