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/render-props.md
+32-33Lines changed: 32 additions & 33 deletions
Original file line number
Diff line number
Diff line change
@@ -4,25 +4,25 @@ title: Render Props
4
4
permalink: docs/render-props.html
5
5
---
6
6
7
-
The term["render prop"](https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce)refers to a technique for sharing code between React components using a prop whose value is a function.
7
+
Thuật ngữ["render prop"](https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce)nói về một kĩ thuật chia sẻ code giữa các React components bằng cách dùng một đối tượng (prop) có giá trị là một hàm (function).
8
8
9
-
A component with a render prop takes a function that returns a React element and calls it instead of implementing its own render logic.
9
+
Một component có một render prop sẽ lấy một hàm trả về một phần tử React (React element) và gọi hàm đó thay vì phải thực hiện render với logic riêng biệt.
10
10
11
11
```jsx
12
12
<DataProvider render={data=> (
13
13
<h1>Hello {data.target}</h1>
14
14
)}/>
15
15
```
16
16
17
-
Libraries that use render props include [React Router](https://reacttraining.com/react-router/web/api/Route/render-func), [Downshift](https://github.com/paypal/downshift)and[Formik](https://github.com/jaredpalmer/formik).
17
+
Những thư viện sử dụng render props gồm có [React Router](https://reacttraining.com/react-router/web/api/Route/render-func), [Downshift](https://github.com/paypal/downshift)và[Formik](https://github.com/jaredpalmer/formik).
18
18
19
-
In this document, we’ll discuss why render props are useful, and how to write your own.
19
+
Ở phần này, chúng ta sẽ bàn luận vì sao render props có thể trở nên hữu ích, và cách để tự viết render props.
20
20
21
-
## Use Render Props for Cross-Cutting Concerns {#use-render-props-for-cross-cutting-concerns}
21
+
## Sử Dụng Render Props Cho Cross-Cutting Concerns {#use-render-props-for-cross-cutting-concerns}
22
22
23
-
Components are the primary unit of code reuse in React, but it's not always obvious how to share the state or behavior that one component encapsulates to other components that need that same state.
23
+
Components là những đơn vị cơ bản để tái sử dụng trong React, nhưng có một sự mơ hồ trong cách chia sẻ kho dữ liệu (state) hay hành vi (behavior) mà một component đóng gói tới các components khác cũng cần kho dữ liệu đó.
24
24
25
-
For example, the following component tracks the mouse position in a web app:
25
+
Ví dụ, component sau đây sẽ theo dõi vị trí con chuột trong một web app:
26
26
27
27
```js
28
28
classMouseTrackerextendsReact.Component {
@@ -50,11 +50,11 @@ class MouseTracker extends React.Component {
50
50
}
51
51
```
52
52
53
-
As the cursor moves around the screen, the component displays its (x, y) coordinates in a`<p>`.
53
+
Khi con trỏ di chuyển xung quanh màn hình, component này sẽ hiển thị tọa độ (x, y) của con trỏ trong tag`<p>`.
54
54
55
-
Now the question is: How can we reuse this behavior in another component? In other words, if another component needs to know about the cursor position, can we encapsulate that behavior so that we can easily share it with that component?
55
+
Câu hỏi phải đặt ra là: Làm sao để chúng biết tái sử dụng hành vi này ở một component khác? Nói cách khác, nếu một component nào đó cũng cần biết vị trí của con trỏ chuột, liệu chúng ta có thể đóng gói (encapsulate) hành vi trên để dễ dàng chia sẻ nó với component này?
56
56
57
-
Since components are the basic unit of code reuse in React, let's try refactoring the code a bit to use a `<Mouse>`component that encapsulates the behavior we need to reuse elsewhere.
57
+
Vì components là đơn vị cơ bản để tái sử dụng trong React, hãy thử thay đổi (refactor) code một chút để có thể dùng một component `<Mouse>`đóng gói được hành vi chúng ta cần tái sử dụng ở nơi khác.
58
58
59
59
```js
60
60
// The <Mouse> component encapsulates the behavior we need...
@@ -95,11 +95,11 @@ class MouseTracker extends React.Component {
95
95
}
96
96
```
97
97
98
-
Now the`<Mouse>`component encapsulates all behavior associated with listening for `mousemove`events and storing the (x, y) position of the cursor, but it's not yet truly reusable.
98
+
Bây giờ`<Mouse>`đã đóng gói được toàn bộ hành vi lắng nghe các sự kiện `mousemove`và chứa tọa độ (x, y) của con trỏ, nhưng component này vẫn chưa thực sự có thể tái sử dụng.
99
99
100
-
For example, let's say we have a `<Cat>`component that renders the image of a cat chasing the mouse around the screen. We might use a `<Cat mouse={{ x, y }}>`prop to tell the component the coordinates of the mouse so it knows where to position the image on the screen.
100
+
Ví dụ, giả sử chúng ta có một component `<Cat>`sẽ render hình ảnh một con mèo đang đuổi chuột quanh màn hình. Chúng ta có thể sẽ sử dụng `<Cat mouse={{ x, y }}>`để cho component này biết được tọa độ của con trỏ chuột và từ đó thay đổi vị trí hình ảnh trên màn hình.
101
101
102
-
As a first pass, you might try rendering the `<Cat>`*inside `<Mouse>`'s `render`method*, like this:
102
+
Lúc đầu, bạn có thể sẽ thử render `<Cat>`*bên trong phương thức `render`của `<Mouse>`* như thế này:
103
103
104
104
```js
105
105
classCatextendsReact.Component {
@@ -153,10 +153,9 @@ class MouseTracker extends React.Component {
153
153
}
154
154
```
155
155
156
-
This approach will work for our specific use case, but we haven't achieved the objective of truly encapsulating the behavior in a reusable way. Now, every time we want the mouse position for a different use case, we have to create a new component (i.e. essentially another `<MouseWithCat>`) that renders something specifically for that use case.
157
-
158
-
Here's where the render prop comes in: Instead of hard-coding a `<Cat>` inside a `<Mouse>` component, and effectively changing its rendered output, we can provide `<Mouse>` with a function prop that it uses to dynamically determine what to render–a render prop.
156
+
Phương pháp này có thể đúng cho trường hợp cụ thể này, nhưng chúng ta vẫn chưa đạt được mục tiêu là hoàn toàn đóng gói hành vi trên để dễ dàng tái sử dụng. Bởi vì hiện tại, mỗi khi chúng ta muốn sử dụng vị trí con trỏ chuột cho một trường hợp nào đó, chúng ta phải tạo một component mới (như là một `<MouseWithCat>` khác) mà sẽ render riêng biệt cho trường hợp đó.
159
157
158
+
Đây là lúc render prop xuất hiện: Thay vì phải code cứng một `<Cat>` bên trong một component `<Mouse>`, và phải thay đổi đầu ra của nó khi render, chúng ta có thể truyền cho `<Mouse>` một function prop (prop là một hàm) để nó có thể quyết định cái gì cần render một cách linh hoạt-render prop chính là nó.
160
159
```js
161
160
classCatextendsReact.Component {
162
161
render() {
@@ -209,13 +208,13 @@ class MouseTracker extends React.Component {
209
208
}
210
209
```
211
210
212
-
Now, instead of effectively cloning the `<Mouse>`component and hard-coding something else in its `render`method to solve for a specific use case, we provide a `render` prop that`<Mouse>`can use to dynamically determine what it renders.
211
+
Lúc này, thay vì phải clone lại component `<Mouse>`và code cứng phương thức `render`của nó để giải quyết một trường hợp cụ thể, chúng ta cung cấp một `render` prop mà`<Mouse>`có thể sử dụng để render một cách linh hoạt.
213
212
214
-
More concretely, **a render prop is a function prop that a component uses to know what to render.**
213
+
Chính xác hơn thì, **một render prop là một function prop được component sử dụng để biết cái gì cần render.**
215
214
216
-
This technique makes the behavior that we need to share extremely portable. To get that behavior, render a `<Mouse>`with a`render` prop that tells it what to render with the current (x, y) of the cursor.
215
+
Kĩ thuật này giúp chúng ta chia sẻ hành vi cần thiết rất dễ dàng. Để lấy được hành vi lúc nãy, ta chỉ phải render `<Mouse>`với một`render` prop cho biết cần phải render gì với (x, y) hiện tại của con trỏ.
217
216
218
-
One interesting thing to note about render props is that you can implement most [higher-order components](/docs/higher-order-components.html) (HOC) using a regular component with a render prop. For example, if you would prefer to have a `withMouse`HOC instead of a`<Mouse>` component, you could easily create one using a regular `<Mouse>`with a render prop:
217
+
Một lưu ý thú vị về render props chính là bạn có thể thực hiện hầu hết [higher-order components](/docs/higher-order-components.html) (HOC) bằng cách dùng một component bình thường cùng với một render prop. Ví dụ, nếu bạn thích việc có HOC `withMouse`hơn là một component`<Mouse>`, bạn có thể dễ dàng tạo một `<Mouse>`bình thường cùng với một render prop:
219
218
220
219
```js
221
220
// If you really want a HOC for some reason, you can easily
@@ -233,21 +232,21 @@ function withMouse(Component) {
233
232
}
234
233
```
235
234
236
-
So using a render prop makes it possible to use either pattern.
235
+
Vậy nên render prop giúp ta có thể sử dụng giải pháp (pattern) nào trong 2 cái cũng được.
237
236
238
-
## Using Props Other Than`render` {#using-props-other-than-render}
237
+
## Sử Dụng Props Ngoài`render` {#using-props-other-than-render}
239
238
240
-
It's important to remember that just because the pattern is called "render props" you don't *have to use a prop named `render`to use this pattern*. In fact, [*any* prop that is a function that a component uses to know what to render is technically a "render prop"](https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce).
239
+
Điều quan trọng cần phải nhớ là, chỉ vì giải pháp này được gọi là "render props", bạn không *nhất thiết phải dùng một prop có tên là `render`mới dùng được giải pháp này*. Thực chất, [*bất kì* prop nào là một hàm mà component sử dụng để biết cái cần để render đều được gọi là một "render prop"](https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce).
241
240
242
-
Although the examples above use `render`, we could just as easily use the `children` prop!
241
+
Mặc dù các ví dụ trên sử dụng `render`, chúng ta có thể gọi prop đó là `children`!
243
242
244
243
```js
245
244
<Mouse children={mouse=> (
246
245
<p>The mouse position is {mouse.x}, {mouse.y}</p>
247
246
)}/>
248
247
```
249
248
250
-
And remember, the`children`prop doesn't actually need to be named in the list of "attributes" in your JSX element. Instead, you can put it directly *inside* the element!
249
+
Và nhớ là, prop`children`này không cần phải được đặt tên trong các "thuộc tính" (attributes) của một phần tử JSX (JSX element). Thay vào đó, bạn có thể đặt nó vào trực tiếp *bên trong* phần tử đó!
251
250
252
251
```js
253
252
<Mouse>
@@ -257,9 +256,9 @@ And remember, the `children` prop doesn't actually need to be named in the list
257
256
</Mouse>
258
257
```
259
258
260
-
You'll see this technique used in the[react-motion](https://github.com/chenglou/react-motion) API.
259
+
Bạn sẽ thấy kĩ thuật này được dùng trong API[react-motion](https://github.com/chenglou/react-motion) API.
261
260
262
-
Since this technique is a little unusual, you'll probably want to explicitly state that`children`should be a function in your `propTypes`when designing an API like this.
261
+
Vì kĩ thuật này khá là lạ, bạn có lẽ sẽ muốn nói rõ`children`là một hàm trong `propTypes`của bạn khi thiết kế một API như vậy.
263
262
264
263
```js
265
264
Mouse.propTypes= {
@@ -269,11 +268,11 @@ Mouse.propTypes = {
269
268
270
269
## Caveats {#caveats}
271
270
272
-
### Be careful when using Render Props with React.PureComponent{#be-careful-when-using-render-props-with-reactpurecomponent}
271
+
### Cẩn thận khi sử dụng Render Props với React.PureComponent{#be-careful-when-using-render-props-with-reactpurecomponent}
273
272
274
-
Using a render prop can negate the advantage that comes from using [`React.PureComponent`](/docs/react-api.html#reactpurecomponent)if you create the function inside a `render` method. This is because the shallow prop comparison will always return`false`for new props, and each`render`in this case will generate a new value for the render prop.
273
+
Sử dụng một render prop có thể phủ nhận lợi thế từ việc sử dụng [`React.PureComponent`](/docs/react-api.html#reactpurecomponent)nếu bạn tạo một hàm bên trong phương thức `render`. Lý do là vì so sánh nông (shallow comparison) prop sẽ luôn trả về`false`cho props mới, và mỗi`render`trong trường hợp này sẽ tạo một giá trị mới cho render prop.
275
274
276
-
For example, continuing with our `<Mouse>` component from above, if`Mouse`were to extend`React.PureComponent`instead of `React.Component`, our example would look like this:
275
+
Ví dụ, tiếp tục với component `<Mouse>`, nếu`Mouse`mở rộng với`React.PureComponent`thay vì với `React.Component`, chúng ta sẽ có như thế này:
277
276
278
277
```js
279
278
classMouseextendsReact.PureComponent {
@@ -299,9 +298,9 @@ class MouseTracker extends React.Component {
299
298
}
300
299
```
301
300
302
-
In this example, each time`<MouseTracker>`renders, it generates a new function as the value of the `<Mouse render>` prop, thus negating the effect of `<Mouse>`extending`React.PureComponent` in the first place!
301
+
Ở ví dụ trên, mỗi lần`<MouseTracker>`render, nó tạo một hàm mới đồng thời là giá trị của prop `<Mouse render>`, từ đó đã phủ nhận công dụng của việc mở rộng `<Mouse>`với`React.PureComponent`!
303
302
304
-
To get around this problem, you can sometimes define the prop as an instance method, like so:
303
+
Để xử lý được rắc rối này, bạn có thể gọi prop thành một instance method (phương thức thể hiện) như sau:
305
304
306
305
```js
307
306
classMouseTrackerextendsReact.Component {
@@ -322,4 +321,4 @@ class MouseTracker extends React.Component {
322
321
}
323
322
```
324
323
325
-
In cases where you cannot define the prop statically (e.g. because you need to close over the component's props and/or state) `<Mouse>`should extend `React.Component` instead.
324
+
Trong những trường hợp bạn không thể gọi prop đó theo cách tĩnh (có thể là vì bạn cần bọc props và/hoặc kho dữ liệu (state) của component đó), bạn nên mở rộng `<Mouse>`với `React.Component`.
0 commit comments