Skip to content

Commit b96e945

Browse files
authored
translating import and export components (#460)
1 parent 02384fa commit b96e945

File tree

2 files changed

+57
-58
lines changed

2 files changed

+57
-58
lines changed

src/content/learn/importing-and-exporting-components.md

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
---
2-
title: Importing and Exporting Components
2+
title: Importing (nhập) và Exporting (Xuất) Components
33
---
44

55
<Intro>
66

7-
The magic of components lies in their reusability: you can create components that are composed of other components. But as you nest more and more components, it often makes sense to start splitting them into different files. This lets you keep your files easy to scan and reuse components in more places.
7+
Ưu điểm của components nằm ở việc bạn có thể tái sử dụng chúng. Bạn có thể tạo ra components mà chứa đựng các components khác. Nhưng nếu bạn muốn lồng ghép càng nhiều components vô lẫn nhau thì nên chia các components của bạn thành các files riêng lẻ. Điều này sẽ giúp bạn thuận tiện trong việc quản lí files để tái sử dụng components ở nhiều vị trí khác nhau.
88

99
</Intro>
1010

1111
<YouWillLearn>
1212

13-
* What a root component file is
14-
* How to import and export a component
15-
* When to use default and named imports and exports
16-
* How to import and export multiple components from one file
17-
* How to split components into multiple files
13+
* Khái niệm về file của root component (gốc)
14+
* Cách để import export một component
15+
* Khi nào nên dùng default (mặc định) hoặc named (đặt tên) imports exports
16+
* Cách để import export nhiều components từ một file
17+
* Cách để chia nhiều components ra các files khác nhau
1818

1919
</YouWillLearn>
2020

21-
## The root component file {/*the-root-component-file*/}
21+
## File của root component {/*the-root-component-file*/}
2222

23-
In [Your First Component](/learn/your-first-component), you made a `Profile` component and a `Gallery` component that renders it:
23+
Trong [Component đầu tiên của bạn](/learn/your-first-component), bạn đã tạo ra một component `Profile` và dùng một component `Gallery` khác để render nó:
2424

2525
<Sandpack>
2626

@@ -52,17 +52,17 @@ img { margin: 0 10px 10px 0; height: 90px; }
5252

5353
</Sandpack>
5454

55-
These currently live in a **root component file,** named `App.js` in this example. Depending on your setup, your root component could be in another file, though. If you use a framework with file-based routing, such as Next.js, your root component will be different for every page.
55+
Trong ví dụ trên, các components đang được đặt trong **file của root component,** với tên file là `App.js`. Tuy nhiên, tuỳ thuộc vào cách setup của bạn, root component cũng có thể được đặt trong một file khác. Nếu bạn sử dụng một framework theo cấu trúc file, như là Next.js, root component sẽ khác nhau cho mỗi trang khác nhau.
5656

57-
## Exporting and importing a component {/*exporting-and-importing-a-component*/}
57+
## Exporting importing một component {/*exporting-and-importing-a-component*/}
5858

59-
What if you want to change the landing screen in the future and put a list of science books there? Or place all the profiles somewhere else? It makes sense to move `Gallery` and `Profile` out of the root component file. This will make them more modular and reusable in other files. You can move a component in three steps:
59+
Giả sử trong trường hợp bạn muốn thay đổi trang giao diện để hiển thị một danh sách về các loại sách khoa học? Hoặc bạn muốn đặt tất cả profiles ở vị trí khác? Phương thức hiệu quả là dời cả components `Gallery` `Profile` ra khỏi file của root component. Cách này sẽ làm chúng dễ tái sử dụng hơn ở các files khác nhau. Bạn có thể dời một component với 3 bước như sau:
6060

61-
1. **Make** a new JS file to put the components in.
62-
2. **Export** your function component from that file (using either [default](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#using_the_default_export) or [named](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#using_named_exports) exports).
63-
3. **Import** it in the file where you’ll use the component (using the corresponding technique for importing [default](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#importing_defaults) or [named](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#import_a_single_export_from_a_module) exports).
61+
1. **Tạo ra** một file JS mới để chứa các components.
62+
2. **Export** function component của bạn ra khỏi file mới tạo (bằng cách sử dụng [default](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#using_the_default_export) hoặc [named](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#using_named_exports) exports).
63+
3. **Import** component đó vào file mà bạn muốn dùng nó (sử dụng phương pháp import tương tự như cách bạn export ở trên với [default](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#importing_defaults) hoặc [named](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#import_a_single_export_from_a_module)).
6464

65-
Here both `Profile` and `Gallery` have been moved out of `App.js` into a new file called `Gallery.js`. Now you can change `App.js` to import `Gallery` from `Gallery.js`:
65+
Ví dụ ở dưới là cách bạn dời cả hai `Profile` `Gallery` ra khỏi `App.js` và đặt chúng vào một file mới tên `Gallery.js`. Bây giờ bạn có thể thay đổi `App.js` để import `Gallery` từ `Gallery.js`:
6666

6767
<Sandpack>
6868

@@ -104,82 +104,81 @@ img { margin: 0 10px 10px 0; height: 90px; }
104104

105105
</Sandpack>
106106

107-
Notice how this example is broken down into two component files now:
107+
Bây giờ, hãy để ý ở ví dụ trên đã chia ra thành hai files component khác nhau, bao gồm:
108108

109109
1. `Gallery.js`:
110-
- Defines the `Profile` component which is only used within the same file and is not exported.
111-
- Exports the `Gallery` component as a **default export.**
110+
- Thiết lập component `Profile` với mục đích sử dụng trong file và không được export ra ngoài.
111+
- Export component `Gallery` bằng cách **default export.**
112112
2. `App.js`:
113-
- Imports `Gallery` as a **default import** from `Gallery.js`.
114-
- Exports the root `App` component as a **default export.**
113+
- Import `Gallery` bằng cách **default import** từ file `Gallery.js`.
114+
- Export root component `App` bằng cách **default export.**
115115

116116

117117
<Note>
118118

119-
You may encounter files that leave off the `.js` file extension like so:
119+
Có những trường hợp mà files bỏ đi phần đuôi `.js` như ví dụ dưới đây:
120120

121121
```js
122122
import Gallery from './Gallery';
123123
```
124124

125-
Either `'./Gallery.js'` or `'./Gallery'` will work with React, though the former is closer to how [native ES Modules](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Modules) work.
125+
Cả `'./Gallery.js'` hoặc `'./Gallery'` đều đúng với React. Tuy nhiên, sử dụng `'./Gallery.js'` sẽ thuận với cách dùng của [native ES Modules](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Modules) hơn.
126126

127127
</Note>
128128

129129
<DeepDive>
130130

131131
#### Default vs named exports {/*default-vs-named-exports*/}
132132

133-
There are two primary ways to export values with JavaScript: default exports and named exports. So far, our examples have only used default exports. But you can use one or both of them in the same file. **A file can have no more than one _default_ export, but it can have as many _named_ exports as you like.**
133+
Có hai cách chính để export giá trị bằng JavaScripts: default exports named exports. Những ví dụ từ đầu tới giờ chỉ dùng default exports. Tuy nhiên bạn có thể sử dụng một hoặc cả hai cách trong cùng một file. **Hãy nhớ rằng mỗi file chỉ được có duy nhất một _default_ export, nhưng nó được phép có nhiều _named_ exports.**
134134

135-
![Default and named exports](/images/docs/illustrations/i_import-export.svg)
135+
![Default named exports](/images/docs/illustrations/i_import-export.svg)
136136

137-
How you export your component dictates how you must import it. You will get an error if you try to import a default export the same way you would a named export! This chart can help you keep track:
137+
Cách bạn export một component sẽ quyết định cách bạn import nó. Bạn sẽ gặp lỗi nếu bạn import một default export theo cách mà bạn dùng cho named export! Bảng hướng dẫn dưới đây sẽ giúp bạn tránh lỗi này:
138138

139-
| Syntax | Export statement | Import statement |
139+
| Cú pháp | Câu lệnh Export | Câu lệnh Import |
140140
| ----------- | ----------- | ----------- |
141141
| Default | `export default function Button() {}` | `import Button from './Button.js';` |
142142
| Named | `export function Button() {}` | `import { Button } from './Button.js';` |
143143

144-
When you write a _default_ import, you can put any name you want after `import`. For example, you could write `import Banana from './Button.js'` instead and it would still provide you with the same default export. In contrast, with named imports, the name has to match on both sides. That's why they are called _named_ imports!
144+
Khi bạn dùng một _default_ import, bạn có thể dùng bất cứ tên gọi nào để đặt ở phía sau từ khoá `import`. Ví dụ như nếu bạn dùng `import Banana from './Button.js'` cho loại default export tương ứng thì bạn cũng sẽ đạt được kết quả tương tự. Tuy nhiên, với named imports, tên gọi phải đồng nhất ở cả hai phía export và import. Đó là lí do tại sao mà chúng được gọi là named imports!
145145

146-
**People often use default exports if the file exports only one component, and use named exports if it exports multiple components and values.** Regardless of which coding style you prefer, always give meaningful names to your component functions and the files that contain them. Components without names, like `export default () => {}`, are discouraged because they make debugging harder.
146+
**Thông thường, mọi người dùng default exports cho các file chỉ export một component, và dùng named exports cho các file export nhiều components và các giá trị khác nhau.** Dù cho bạn có dùng bất kể phương pháp nào, hãy nhớ là luôn luôn đặt tên mang tính gợi ý cho các component functions của bạn và cho các files của chúng. Không nên sử dụng components không tên, chẳng hạn như `export default () => {}`, vì chúng sẽ gây khó khăn trong việc sửa lỗi code.
147147

148148
</DeepDive>
149149

150-
## Exporting and importing multiple components from the same file {/*exporting-and-importing-multiple-components-from-the-same-file*/}
151-
152-
What if you want to show just one `Profile` instead of a gallery? You can export the `Profile` component, too. But `Gallery.js` already has a *default* export, and you can't have _two_ default exports. You could create a new file with a default export, or you could add a *named* export for `Profile`. **A file can only have one default export, but it can have numerous named exports!**
150+
## Exporting và importing nhiều components từ cùng một file {/*exporting-and-importing-multiple-components-from-the-same-file*/}
153151

152+
Giả sử bạn muốn hiển thị chỉ một `Profile` thay vì là cả một gallery? Bạn có thể export component `Profile`. Tuy nhiên, `Gallery.js` đã có dùng một *default* export rồi, và bạn không được dùng _hai_ default exports trong một file. Thế nên có hai cách làm như sau là (1) bạn có thể tạo ra một file mới rồi dùng default export, hoặc (2) bạn có thể dùng một *named* export cho component `Profile`. **Một file chỉ có thể dùng một default export, nhưng nó có thể dùng nhiều named exports!**
154153
<Note>
155154

156-
To reduce the potential confusion between default and named exports, some teams choose to only stick to one style (default or named), or avoid mixing them in a single file. Do what works best for you!
155+
Để giảm bớt việc lẫn lộn giữa default named exports, một số teams chọn chỉ sử dụng một trong hai (default hoặc named), hoặc tránh việc dùng lẫn cả hai trong cùng một file. Hãy chọn cách hiệu quả nhất cho bạn!
157156

158157
</Note>
159158

160-
First, **export** `Profile` from `Gallery.js` using a named export (no `default` keyword):
159+
Bước đầu tiên, **export** `Profile` từ `Gallery.js` bằng named export (bỏ từ `default` ra):
161160

162161
```js
163162
export function Profile() {
164163
// ...
165164
}
166165
```
167166

168-
Then, **import** `Profile` from `Gallery.js` to `App.js` using a named import (with the curly braces):
167+
Sau đó, **import** `Profile` từ `Gallery.js` vào `App.js` bằng named import (sử dụng cặp dấu ngoặc nhọn):
169168

170169
```js
171170
import { Profile } from './Gallery.js';
172171
```
173172

174-
Finally, **render** `<Profile />` from the `App` component:
173+
Bước cuối cùng, **render** `<Profile />` từ component `App`:
175174

176175
```js
177176
export default function App() {
178177
return <Profile />;
179178
}
180179
```
181180

182-
Now `Gallery.js` contains two exports: a default `Gallery` export, and a named `Profile` export. `App.js` imports both of them. Try editing `<Profile />` to `<Gallery />` and back in this example:
181+
Sau khi hoàn thành các bước trên thì hiện tại `Gallery.js` chứa hai components: một default export `Gallery`, và một named export `Profile`. `App.js` import cả hai components trên. Ở ví dụ dưới đây, bạn hãy thử đổi `<Profile />` thành `<Gallery />`, và ngược lại.
183182

184183
<Sandpack>
185184

@@ -222,47 +221,47 @@ img { margin: 0 10px 10px 0; height: 90px; }
222221

223222
</Sandpack>
224223

225-
Now you're using a mix of default and named exports:
224+
Hiện tại, bạn đang sử dụng xen lẫn cả default named exports:
226225

227226
* `Gallery.js`:
228-
- Exports the `Profile` component as a **named export called `Profile`.**
229-
- Exports the `Gallery` component as a **default export.**
227+
- Export component `Profile` bằng cách **named export với tên `Profile`.**
228+
- Export component `Gallery` bằng cách **default export.**
230229
* `App.js`:
231-
- Imports `Profile` as a **named import called `Profile`** from `Gallery.js`.
232-
- Imports `Gallery` as a **default import** from `Gallery.js`.
233-
- Exports the root `App` component as a **default export.**
230+
- Import `Profile` bằng cách **named import với tên `Profile`** từ `Gallery.js`.
231+
- Import `Gallery` bằng cách **default import** từ `Gallery.js`.
232+
- Export root component `App` bằng cách **default export.**
234233

235234
<Recap>
236235

237-
On this page you learned:
236+
Trong chương này, bạn đã học được:
238237

239-
* What a root component file is
240-
* How to import and export a component
241-
* When and how to use default and named imports and exports
242-
* How to export multiple components from the same file
238+
* Khái niệm về file của root component
239+
* Cách để import export một component
240+
* Khi nào và làm sao để sử dụng default named imports exports
241+
* Làm sao để export nhiều components từ cùng một file
243242

244243
</Recap>
245244

246245

247246

248247
<Challenges>
249248

250-
#### Split the components further {/*split-the-components-further*/}
249+
#### Tách riêng các components thêm hơn nữa {/*split-the-components-further*/}
251250

252-
Currently, `Gallery.js` exports both `Profile` and `Gallery`, which is a bit confusing.
251+
Hiện tại, việc `Gallery.js` export cả hai components `Profile` `Gallery` đôi khi có thể gây nhầm lẫn.
253252

254-
Move the `Profile` component to its own `Profile.js`, and then change the `App` component to render both `<Profile />` and `<Gallery />` one after another.
253+
Ta có thể dời component `Profile` vào một file riêng khác với tên `Profile.js`, sau đó dùng component `App` để lần lượt render cả hai `<Profile />` `<Gallery />`.
255254

256-
You may use either a default or a named export for `Profile`, but make sure that you use the corresponding import syntax in both `App.js` and `Gallery.js`! You can refer to the table from the deep dive above:
255+
Bạn có thể sử dụng default hoặc named export cho `Profile`, tuy nhiên hãy nhớ sử dụng cú pháp import cho tương ứng trong cả hai file `App.js` `Gallery.js`! Bạn có thể xem bảng hướng dẫn phía dưới cho cách sử dụng cú pháp chính xác:
257256

258-
| Syntax | Export statement | Import statement |
257+
| Cú pháp | Câu lệnh Export | Câu lệnh Import |
259258
| ----------- | ----------- | ----------- |
260259
| Default | `export default function Button() {}` | `import Button from './Button.js';` |
261260
| Named | `export function Button() {}` | `import { Button } from './Button.js';` |
262261

263262
<Hint>
264263

265-
Don't forget to import your components where they are called. Doesn't `Gallery` use `Profile`, too?
264+
Đừng quên import components ở nơi mà chúng được dùng. Cân nhắc xem `Gallery` có dùng `Profile` hay không?
266265

267266
</Hint>
268267

@@ -313,11 +312,11 @@ img { margin: 0 10px 10px 0; height: 90px; }
313312

314313
</Sandpack>
315314

316-
After you get it working with one kind of exports, make it work with the other kind.
315+
Sau khi bạn thành công với một phương pháp export, hãy thử với phương pháp còn lại.
317316

318317
<Solution>
319318

320-
This is the solution with named exports:
319+
Dưới đây là đáp án mà sử dụng named exports:
321320

322321
<Sandpack>
323322

@@ -367,7 +366,7 @@ img { margin: 0 10px 10px 0; height: 90px; }
367366

368367
</Sandpack>
369368

370-
This is the solution with default exports:
369+
Dưới đây là đáp án mà sử dụng default exports:
371370

372371
<Sandpack>
373372

src/sidebarLearn.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"path": "/learn/your-first-component"
7676
},
7777
{
78-
"title": "Importing and Exporting Components",
78+
"title": "Importing Exporting Components",
7979
"path": "/learn/importing-and-exporting-components"
8080
},
8181
{

0 commit comments

Comments
 (0)