Class 02: Import/Export component
Giới thiệu
Export component mặc định (default export)
Cú pháp
// app/components/Header.tsx
'use client';
const Header = () => {
return <header>Đây là header</header>;
};
export default Header;Cách import component mặc định
// app/page.tsx
import Header from './components/Header';
export default function HomePage() {
return (
<div>
<Header />
</div>
);
}Lưu ý
Export component theo tên (named export)
Cú pháp
Cách import component theo tên
Lưu ý
Kết hợp default và named export
Ví dụ
Cách import
Tổ chức component bằng thư mục
Cấu trúc thư mục
Nội dung từng file
Import từ index.ts
index.tsLợi ích
Import component từ thư viện bên ngoài
Bài tập thực hành cho học viên
Bài tập 1: Tạo 3 component và export theo kiểu named export
Bài tập 2: Tạo thư mục common chứa nhiều component và dùng index.ts
common chứa nhiều component và dùng index.tsBài tập 3: Tạo một file component chính có default export, thêm một component phụ dùng named export
Tổng kết
Last updated