# Export và Import

## Giới thiệu

ES6 cung cấp cho chúng ta **import (nhập)**, **export (xuất)** các functions, biến từ module này sang module khác và sử dụng nó trong các file khác. Nói một cách chính xác theo thuật ngữ React, người ta có thể sử dụng các stateless components trong các components khác bằng cách export các components từ các modules tương ứng và sử dụng nó trong các tệp khác.

ES6 cung cấp hai cách để export một module từ một tệp: named export và default export.<br>

#### 1. Named Export: (export): <a href="#id-1-named-export-export-1" id="id-1-named-export-export-1"></a>

Trong JavaScript ES6, named export được sử dụng để xuất nhiều thứ từ một module bằng cách thêm keyword export vào khai báo của chúng. Những thứ được export sẽ được phân biệt bằng tên. Sau đó import những thứ chúng ta cần sử dụng bằng cách bao quanh chúng cặp dấu ngoặc nhọn **{ }**. Tên của module đã nhập phải giống với tên của module đã xuất.

Ví dụ 1: Tôi đã tạo các hàm được đặt tên trong một tệp JavaScript có tên là functionsFile.js

![](https://89779819-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8jEJk3DAzRpg9xgL2w%2F-M9MwMN8ICICyjfUtHo3%2F-M9MxoWh3H57lLI08t45%2FA%CC%89nh%20chu%CC%A3p%20Ma%CC%80n%20hi%CC%80nh%202020-06-09%20lu%CC%81c%2014.20.52.png?alt=media\&token=0e24d46b-d85b-4a1c-bbd1-90e499dc21f9)

Bây giờ hàm SquareNumber và biến pi đã sẵn sàng để import. Tôi sẽ tạo một tệp có tên main.js và import các giá trị được export ở trên

![](https://89779819-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8jEJk3DAzRpg9xgL2w%2F-M9Mxw6uw-bWPr78yD3O%2F-M9My2M4VFzLcHrcO6IS%2FA%CC%89nh%20chu%CC%A3p%20Ma%CC%80n%20hi%CC%80nh%202020-06-09%20lu%CC%81c%2014.24.51.png?alt=media\&token=9e42e07d-dfac-49ed-9c8e-4db063d9c816)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://haonm.gitbook.io/react-the-complete-guide/export-va-import.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
