1. Giới thiệu
Khi quay lại làm frontend sau một thời gian dài, việc bắt đầu ngay bằng cách viết từng React Component có thể khiến chúng ta gặp khó khăn. Bạn phải đồng thời suy nghĩ về giao diện, nhớ Bootstrap classes, JSX syntax và cách tổ chức component.
Thay vì làm tất cả cùng lúc, chúng ta sẽ áp dụng một quy trình đơn giản hơn:
- Thiết kế một trang HTML hoàn chỉnh bằng Bootstrap 5.
- Phân tích cấu trúc giao diện và xác định các khu vực có thể tách thành component.
- Chuyển từng khu vực HTML sang React Components.
- Tách CSS riêng cho từng component.
- Ghép các component lại thành một trang Dashboard hoàn chỉnh.
Trong bài này, chúng ta sẽ xây dựng BookNest – Personal Book Library, một ứng dụng quản lý thư viện sách cá nhân.
Giao diện bao gồm:
- Header: Logo, Search, Notification và User Profile.
- Sidebar: Dashboard, My Books, Reading List, Categories, Statistics và Settings.
- Main Content: Welcome Banner, Reading Statistics, Currently Reading, Recently Added Books và Reading Goal.
Chúng ta sẽ sử dụng Bootstrap utilities để xây dựng giao diện, không cần tự viết CSS ở giai đoạn HTML Static.
2. Khởi tạo React + TypeScript Project
2.1. Chuẩn bị môi trường
Các công cụ cần có:
| Công cụ | Mục đích |
|---|---|
| Node.js | Runtime cho JavaScript |
| Yarn | Package manager |
| Visual Studio Code | Code editor |
| React | UI library |
| TypeScript | Static type checking |
| Vite | Development server và build tool |
| Bootstrap 5 | CSS framework |
Kiểm tra Node.js và Yarn:
node -v
yarn -v
Nếu chưa có Yarn, có thể kích hoạt Corepack nếu phiên bản Node.js hỗ trợ:
corepack enable
yarn -v
Thiết lập dùng node-modules thay vì PnP (Plug'n'Play) (Chạy lệnh sau một lần duy nhất ở bất kỳ đâu trên máy tính của bạn)
yarn config set --home nodeLinker node-modules
2.2. Tạo project bằng Vite
Mở terminal tại thư mục muốn lưu project và chạy:
> yarn create vite booknest --template react-ts
➤ YN0000: · Yarn 4.18.1
➤ YN0000: ┌ Resolution step
➤ YN0085: │ + create-vite@npm:9.3.4
➤ YN0000: └ Completed
➤ YN0000: ┌ Fetch step
➤ YN0013: │ A package was added to the project (+ 887.04 KiB).
➤ YN0000: └ Completed
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed in 0s 245ms
➤ YN0000: · Done in 0s 277ms
create-vite v9.3.4
│
◇ Which linter to use?
│ ESLint
│
◇ Install with yarn and start now?
│ Yes
│
◇ Scaffolding project in D:\Practices\ReactJs\booknest...
│
◇ Installing dependencies with yarn...
➤ YN0000: · Yarn 4.18.1
➤ YN0000: ┌ Resolution step
➤ YN0085: │ + @eslint/js@npm:10.0.1, @types/node@npm:24.19.2, @types/react-dom@npm:19.3.0, and 209 more.
➤ YN0000: └ Completed in 0s 931ms
➤ YN0000: ┌ Fetch step
➤ YN0013: │ 7 packages were added to the project (+ 4.45 MiB).
➤ YN0000: └ Completed in 0s 618ms
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed in 2s 5ms
➤ YN0000: · Done in 3s 588ms
│
◇ Starting dev server...
Giải thích:
| Thành phần | Ý nghĩa |
|---|---|
yarn create vite | Sử dụng Vite để tạo project. |
booknest | Tên project. |
--template | Chọn project template. |
react-ts | Template React + TypeScript. |
Di chuyển vào thư mục project:
cd booknest
Cài đặt dependencies:
yarn install
Lệnh này cài các package được khai báo trong package.json vào thư mục node_modules.
Chạy thử ứng dụng:
yarn dev
Vite sẽ cung cấp địa chỉ development server, thông thường là http://localhost:5173/.
2.3. Cài đặt Bootstrap bằng Yarn
Cài Bootstrap và Bootstrap Icons:
yarn add bootstrap bootstrap-icons
Hai package sẽ được cài vào node_modules và được khai báo trong package.json.
| Package | Công dụng |
|---|---|
bootstrap | Bootstrap CSS framework và JavaScript components. |
bootstrap-icons | Bộ icon của Bootstrap. |
Vì sao nên cài package thay vì sử dụng CDN?
- Quản lý dependency và phiên bản ngay trong project.
- Import package trực tiếp trong source code.
- Vite có thể xử lý dependency khi build production.
- Không cần phụ thuộc CDN trong quá trình triển khai.
2.4. Cấu trúc project ban đầu
booknest/
├── node_modules/
│ ├── bootstrap/
│ └── bootstrap-icons/
├── public/
├── src/
│ ├── assets/
│ ├── App.css
│ ├── App.tsx
│ ├── index.css
│ └── main.tsx
├── index.html
├── package.json
├── tsconfig.json
├── vite.config.ts
└── yarn.lock
Chúng ta sẽ giữ nguyên cấu trúc Vite ban đầu trong giai đoạn khởi tạo. Các thư mục components và pages sẽ được tạo khi bắt đầu phân rã giao diện.
3. Thiết kế HTML Static Template
3.1. Phân tích giao diện trước khi code
Trước khi viết HTML, hãy xác định bố cục tổng thể:
┌──────────────────────────────────────────────────────┐
│ HEADER │
│ Logo Search Notification User │
├───────────────┬──────────────────────────────────────┤
│ │ │
│ SIDEBAR │ CONTENT │
│ │ │
│ Dashboard │ Welcome Banner │
│ My Books │ │
│ Reading List │ Reading Statistics │
│ Categories │ │
│ Statistics │ Currently Reading │
│ Settings │ │
│ │ Recently Added Books │
│ │ │
│ │ Reading Goal │
└───────────────┴──────────────────────────────────────┘
Đây là cấu trúc quen thuộc của một SaaS Dashboard. Chúng ta sẽ sử dụng Bootstrap Grid System để chia Sidebar và Content thành hai cột.
3.2. Tạo file HTML độc lập
Tại thư mục gốc project, tạo file:
booknest-static.html
Đây là file HTML tham khảo độc lập, không phải file index.html mà Vite sử dụng.
Vì file nằm ở thư mục gốc, chúng ta có thể tham chiếu CSS từ node_modules bằng đường dẫn tương đối:
<link rel="stylesheet"
href="./node_modules/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet"
href="./node_modules/bootstrap-icons/font/bootstrap-icons.css">
3.3. HTML Template hoàn chỉnh
Copy toàn bộ nội dung sau vào booknest-static.html.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BookNest - Personal Book Library</title>
<link rel="stylesheet"
href="./node_modules/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet"
href="./node_modules/bootstrap-icons/font/bootstrap-icons.css">
</head>
<body class="bg-body-tertiary">
<!-- HEADER -->
<header class="bg-white border-bottom">
<nav class="navbar navbar-expand-lg">
<div class="container-fluid px-4">
<a href="#" class="navbar-brand d-flex align-items-center gap-2">
<span class="bg-primary text-white rounded-3 p-2">
<i class="bi bi-book-half fs-4"></i>
</span>
<span class="fw-bold fs-4 text-primary">
BookNest
</span>
</a>
<form class="d-none d-md-flex flex-grow-1 mx-lg-5"
role="search">
<div class="input-group">
<span class="input-group-text bg-light border-end-0">
<i class="bi bi-search"></i>
</span>
<input type="search"
class="form-control bg-light border-start-0"
placeholder="Search your books..."
aria-label="Search books">
</div>
</form>
<div class="d-flex align-items-center gap-3 ms-auto">
<button class="btn btn-light rounded-circle position-relative"
type="button"
aria-label="Notifications">
<i class="bi bi-bell fs-5"></i>
<span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger">
3
</span>
</button>
<div class="d-flex align-items-center gap-2">
<div class="rounded-circle bg-primary text-white d-flex align-items-center justify-content-center"
style="width: 40px; height: 40px;">
AB
</div>
<div class="d-none d-sm-block">
<div class="fw-semibold">An Bình</div>
<small class="text-secondary">Book Lover</small>
</div>
<i class="bi bi-chevron-down text-secondary"></i>
</div>
</div>
</div>
</nav>
</header>
<!-- APPLICATION BODY -->
<div class="container-fluid">
<div class="row">
<!-- SIDEBAR -->
<aside class="col-12 col-lg-2 bg-white border-end p-3">
<div class="text-uppercase small fw-bold text-secondary mb-3 px-2">
Library
</div>
<nav class="nav flex-column gap-2">
<a href="#" class="nav-link active bg-primary text-white rounded-3">
<i class="bi bi-grid me-2"></i>Dashboard
</a>
<a href="#" class="nav-link text-dark">
<i class="bi bi-book me-2"></i>My Books
</a>
<a href="#" class="nav-link text-dark">
<i class="bi bi-bookmark me-2"></i>Reading List
</a>
<a href="#" class="nav-link text-dark">
<i class="bi bi-tags me-2"></i>Categories
</a>
<a href="#" class="nav-link text-dark">
<i class="bi bi-bar-chart me-2"></i>Statistics
</a>
<a href="#" class="nav-link text-dark">
<i class="bi bi-gear me-2"></i>Settings
</a>
</nav>
<hr class="my-4">
<div class="bg-primary-subtle rounded-3 p-3">
<i class="bi bi-trophy text-primary fs-2"></i>
<h6 class="fw-bold mt-2">Reading Goal</h6>
<p class="small text-secondary mb-2">
You are doing great!
</p>
<div class="progress mb-2"
role="progressbar"
aria-label="Annual reading goal"
aria-valuenow="60"
aria-valuemin="0"
aria-valuemax="100">
<div class="progress-bar" style="width: 60%"></div>
</div>
<small class="fw-semibold">12 / 20 Books</small>
</div>
</aside>
<!-- MAIN CONTENT -->
<main class="col-12 col-lg-10 p-3 p-lg-4">
<!-- WELCOME BANNER -->
<section class="bg-primary text-white rounded-4 p-4 mb-4">
<div class="row align-items-center">
<div class="col-md-8">
<p class="text-white-50 mb-2">
SUNDAY, NOVEMBER 15, 2026
</p>
<h1 class="fw-bold">
Welcome back, An Bình!
</h1>
<p class="mb-0">
Every book is a new journey.
What will you read today?
</p>
</div>
<div class="col-md-4 text-md-end mt-3 mt-md-0">
<i class="bi bi-book-half display-1"></i>
</div>
</div>
</section>
<!-- READING STATISTICS -->
<section class="mb-4">
<h2 class="h4 fw-bold mb-3">Reading Overview</h2>
<div class="row g-3">
<div class="col-12 col-md-4">
<article class="card border-0 shadow-sm h-100">
<div class="card-body d-flex align-items-center gap-3">
<div class="bg-primary-subtle text-primary rounded-3 p-3">
<i class="bi bi-book fs-3"></i>
</div>
<div>
<h3 class="h3 fw-bold mb-0">48</h3>
<p class="text-secondary mb-0">Total Books</p>
</div>
</div>
</article>
</div>
<div class="col-12 col-md-4">
<article class="card border-0 shadow-sm h-100">
<div class="card-body d-flex align-items-center gap-3">
<div class="bg-success-subtle text-success rounded-3 p-3">
<i class="bi bi-check-circle fs-3"></i>
</div>
<div>
<h3 class="h3 fw-bold mb-0">12</h3>
<p class="text-secondary mb-0">Completed Books</p>
</div>
</div>
</article>
</div>
<div class="col-12 col-md-4">
<article class="card border-0 shadow-sm h-100">
<div class="card-body d-flex align-items-center gap-3">
<div class="bg-warning-subtle text-warning rounded-3 p-3">
<i class="bi bi-journal-bookmark fs-3"></i>
</div>
<div>
<h3 class="h3 fw-bold mb-0">5</h3>
<p class="text-secondary mb-0">Currently Reading</p>
</div>
</div>
</article>
</div>
</div>
</section>
<!-- CURRENTLY READING -->
<section class="mb-4">
<div class="d-flex justify-content-between align-items-center mb-3">
<h2 class="h4 fw-bold mb-0">Currently Reading</h2>
<a href="#" class="text-decoration-none">View All</a>
</div>
<div class="row g-3">
<div class="col-12 col-md-6">
<article class="card border-0 shadow-sm h-100">
<div class="card-body">
<div class="d-flex gap-3">
<div class="bg-primary-subtle rounded-3 p-4 d-flex align-items-center justify-content-center">
<i class="bi bi-journal-richtext display-5 text-primary"></i>
</div>
<div class="flex-grow-1">
<span class="badge text-bg-primary mb-2">Technology</span>
<h3 class="h5 fw-bold">Clean Code</h3>
<p class="text-secondary mb-2">Robert C. Martin</p>
<div class="d-flex justify-content-between small mb-2">
<span>Reading Progress</span>
<strong>65%</strong>
</div>
<div class="progress"
role="progressbar"
aria-label="Clean Code reading progress"
aria-valuenow="65"
aria-valuemin="0"
aria-valuemax="100">
<div class="progress-bar" style="width: 65%"></div>
</div>
</div>
</div>
</div>
</article>
</div>
<div class="col-12 col-md-6">
<article class="card border-0 shadow-sm h-100">
<div class="card-body">
<div class="d-flex gap-3">
<div class="bg-success-subtle rounded-3 p-4 d-flex align-items-center justify-content-center">
<i class="bi bi-journal-richtext display-5 text-success"></i>
</div>
<div class="flex-grow-1">
<span class="badge text-bg-success mb-2">Personal Development</span>
<h3 class="h5 fw-bold">Atomic Habits</h3>
<p class="text-secondary mb-2">James Clear</p>
<div class="d-flex justify-content-between small mb-2">
<span>Reading Progress</span>
<strong>30%</strong>
</div>
<div class="progress"
role="progressbar"
aria-label="Atomic Habits reading progress"
aria-valuenow="30"
aria-valuemin="0"
aria-valuemax="100">
<div class="progress-bar bg-success" style="width: 30%"></div>
</div>
</div>
</div>
</div>
</article>
</div>
</div>
</section>
<!-- RECENTLY ADDED -->
<section class="mb-4">
<div class="d-flex justify-content-between align-items-center mb-3">
<h2 class="h4 fw-bold mb-0">Recently Added</h2>
<a href="#" class="text-decoration-none">Browse Library</a>
</div>
<div class="card border-0 shadow-sm">
<div class="card-body">
<div class="row g-3">
<div class="col-12 col-md-4">
<div class="d-flex align-items-center gap-3">
<div class="bg-warning-subtle rounded-3 p-3">
<i class="bi bi-book-half fs-2 text-warning"></i>
</div>
<div>
<h3 class="h6 fw-bold mb-1">The Pragmatic Programmer</h3>
<small class="text-secondary">Andrew Hunt</small>
</div>
</div>
</div>
<div class="col-12 col-md-4">
<div class="d-flex align-items-center gap-3">
<div class="bg-info-subtle rounded-3 p-3">
<i class="bi bi-book-half fs-2 text-info"></i>
</div>
<div>
<h3 class="h6 fw-bold mb-1">Deep Work</h3>
<small class="text-secondary">Cal Newport</small>
</div>
</div>
</div>
<div class="col-12 col-md-4">
<div class="d-flex align-items-center gap-3">
<div class="bg-danger-subtle rounded-3 p-3">
<i class="bi bi-book-half fs-2 text-danger"></i>
</div>
<div>
<h3 class="h6 fw-bold mb-1">Thinking, Fast and Slow</h3>
<small class="text-secondary">Daniel Kahneman</small>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<!-- READING GOAL -->
<section class="card border-0 shadow-sm">
<div class="card-body p-4">
<div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-3">
<div>
<h2 class="h5 fw-bold">
<i class="bi bi-trophy text-warning me-2"></i>
Your 2026 Reading Challenge
</h2>
<p class="text-secondary mb-0">
You have completed 12 out of 20 books.
Keep going!
</p>
</div>
<div class="text-md-end">
<div class="display-6 fw-bold text-primary">60%</div>
<small class="text-secondary">Annual Goal</small>
</div>
</div>
</div>
</section>
</main>
</div>
</div>
</body>
</html>
3.4. Mở HTML trên trình duyệt
Mở file booknest-static.html trực tiếp bằng Chrome hoặc Firefox.
Ở bước này, bạn đã có một trang Dashboard hoàn chỉnh mà chưa cần viết bất kỳ React Component nào.
Chúng ta sẽ giữ file HTML này làm bản tham chiếu trong suốt quá trình chuyển đổi.
4. Hiểu Bootstrap Layout trước khi chuyển sang React
4.1. Bootstrap Grid System
Bootstrap sử dụng hệ thống Grid gồm 12 cột.
Trong BookNest, phần Body được tổ chức như sau:
<div class="container-fluid">
<div class="row">
<aside class="col-12 col-lg-2">
Sidebar
</aside>
<main class="col-12 col-lg-10">
Main Content
</main>
</div>
</div>
Giải thích:
container-fluid: Container chiếm toàn bộ chiều rộng.row: Tạo một hàng trong Grid System.col-12: Chiếm 12/12 cột ở breakpoint mặc định.col-lg-2: Chiếm 2/12 cột từ breakpoint LG.col-lg-10: Chiếm 10/12 cột từ breakpoint LG.
Ở màn hình Desktop, Sidebar chiếm 2 cột và Content chiếm 10 cột. Ở màn hình nhỏ, cả hai đều chiếm toàn bộ chiều rộng.
4.2. Bootstrap Breakpoints
| Prefix | Breakpoint |
|---|---|
col- | Mặc định, dưới 576px |
col-sm- | ≥ 576px |
col-md- | ≥ 768px |
col-lg- | ≥ 992px |
col-xl- | ≥ 1200px |
col-xxl- | ≥ 1400px |
Ví dụ:
<div class="col-12 col-md-6 col-lg-4">
Book Card
</div>
Ý nghĩa:
- Mobile: chiếm 12 cột.
- Tablet: chiếm 6 cột.
- Desktop: chiếm 4 cột.
4.3. Flexbox Utilities
Bootstrap cung cấp các class để điều khiển Flexbox mà không cần tự viết CSS.
<div class="d-flex align-items-center justify-content-between">
<h2>Currently Reading</h2>
<a href="#">View All</a>
</div>
| Class | Công dụng |
|---|---|
d-flex | Kích hoạt Flexbox. |
align-items-center | Căn giữa theo trục cross-axis. |
justify-content-between | Đưa các phần tử về hai phía. |
flex-column | Xếp các phần tử theo chiều dọc. |
gap-3 | Tạo khoảng cách giữa các phần tử. |
4.4. Spacing Utilities
Bootstrap cung cấp các class điều khiển Margin và Padding:
| Class | Công dụng |
|---|---|
p-3 | Padding tất cả các phía. |
px-4 | Padding trái và phải. |
py-2 | Padding trên và dưới. |
mb-4 | Margin bottom. |
mt-3 | Margin top. |
mx-auto | Margin trái và phải tự động. |
Hiểu các nhóm class này sẽ giúp bạn đọc và chuyển đổi HTML sang JSX dễ dàng hơn.
5. Phân tích giao diện và xác định React Components
Trước khi chuyển đổi, chúng ta cần xác định các khu vực có trách nhiệm riêng.
5.1. Application Layout
Đây là cấu trúc tổng thể của ứng dụng:
Application
├── Header
└── Application Body
├── Sidebar
└── Main Content
5.2. Dashboard Sections
Bên trong Main Content có các khu vực:
DashboardPage
├── WelcomeBanner
├── StatisticsSection
├── CurrentlyReadingSection
├── RecentlyAddedSection
└── ReadingGoalSection
5.3. Reusable Components
Tiếp tục quan sát các phần tử lặp lại:
- Statistics có ba Card cùng cấu trúc.
- Currently Reading có hai Book Card.
- Recently Added có nhiều Book Item.
Đây là những phần phù hợp để tách thành component có thể tái sử dụng.
5.4. Component Tree
App
├── Header
├── Sidebar
└── DashboardPage
├── WelcomeBanner
├── StatisticsSection
│ └── StatCard
├── CurrentlyReadingSection
│ └── BookCard
├── RecentlyAddedSection
│ └── BookItem
└── ReadingGoalSection
6. Tổ chức thư mục React Components
Chúng ta sẽ tổ chức project theo hướng tách Layout, Pages và Components.
src/
├── components/
│ ├── layout/
│ │ ├── Header.tsx
│ │ ├── Header.css
│ │ ├── Sidebar.tsx
│ │ └── Sidebar.css
│ │
│ └── dashboard/
│ ├── WelcomeBanner.tsx
│ ├── WelcomeBanner.css
│ ├── StatCard.tsx
│ ├── StatCard.css
│ ├── StatisticsSection.tsx
│ ├── StatisticsSection.css
│ ├── BookCard.tsx
│ ├── BookCard.css
│ ├── CurrentlyReadingSection.tsx
│ ├── RecentlyAddedSection.tsx
│ └── ReadingGoalSection.tsx
│
├── pages/
│ ├── DashboardPage.tsx
│ └── DashboardPage.css
│
├── App.tsx
├── App.css
├── index.css
└── main.tsx
Một React Component có thể được tổ chức thành hai file chính:
- Component.tsx: Chứa cấu trúc giao diện bằng JSX và logic của component.
- Component.css: Chứa các CSS rules phục vụ riêng cho giao diện component đó.
Ví dụ, Header.tsx sẽ đi cùng Header.css.
7. Tạo React Component đầu tiên: Header
Đây là bước quan trọng vì chúng ta sẽ chuyển từ tư duy viết một trang HTML lớn sang tổ chức giao diện thành những component độc lập.
7.1. Một Component gồm những gì?
Với cách tổ chức CSS thông thường, một component có thể gồm:
Header/
├── Header.tsx
└── Header.css
Header.tsx chịu trách nhiệm:
- Định nghĩa React Component.
- Trả về JSX mô tả cấu trúc HTML.
- Nhận Props nếu cần dữ liệu từ component cha.
- Xử lý sự kiện và logic liên quan đến Header.
- Import stylesheet tương ứng.
Header.css chịu trách nhiệm:
- Định nghĩa màu sắc, kích thước và khoảng cách riêng.
- Điều chỉnh cách hiển thị của Header.
- Quản lý các trạng thái giao diện như hover hoặc responsive khi cần.
Việc tách hai file giúp cấu trúc code dễ đọc, dễ bảo trì và tránh đặt toàn bộ CSS vào một file lớn.
Lưu ý: CSS được import từ file .css thông thường vẫn là global CSS, không tự động được giới hạn trong component. Vì vậy, nên đặt tên class có tiền tố riêng như .app-header, .app-header__brand để tránh trùng tên. Nếu cần CSS thực sự được module hóa, có thể sử dụng CSS Modules với file .module.css.
7.2. Tạo Header.css
Tạo file:
src/components/layout/Header.css
Trong HTML Static, chúng ta sử dụng Bootstrap utilities để tạo giao diện. Khi chuyển sang React, những style đặc thù của Header sẽ được đặt trong file CSS riêng.
.app-header {
min-height: 72px;
}
.app-header__brand {
text-decoration: none;
}
.app-header__logo {
width: 40px;
height: 40px;
}
.app-header__avatar {
width: 40px;
height: 40px;
flex-shrink: 0;
}
Giải thích:
| Selector | Công dụng |
|---|---|
.app-header | Định nghĩa kích thước tối thiểu của Header. |
.app-header__brand | Điều chỉnh phần thương hiệu. |
.app-header__logo | Kích thước vùng logo. |
.app-header__avatar | Kích thước Avatar. |
Chúng ta vẫn sử dụng Bootstrap classes cho layout và utilities. File CSS chỉ chứa những quy tắc đặc thù không muốn viết trực tiếp bằng inline style.
7.3. Tạo Header.tsx
Tạo file:
src/components/layout/Header.tsx
Code:
import './Header.css'
function Header() {
return (
<header className="app-header bg-white border-bottom">
<nav className="navbar navbar-expand-lg h-100">
<div className="container-fluid px-4">
{/* Brand */}
<a
href="#"
className="navbar-brand app-header__brand d-flex align-items-center gap-2"
>
<span className="app-header__logo bg-primary text-white rounded-3 d-flex align-items-center justify-content-center">
<i className="bi bi-book-half fs-4"></i>
</span>
<span className="fw-bold fs-4 text-primary">
BookNest
</span>
</a>
{/* Search */}
<form
className="d-none d-md-flex flex-grow-1 mx-lg-5"
role="search"
>
<div className="input-group">
<span className="input-group-text bg-light border-end-0">
<i className="bi bi-search"></i>
</span>
<input
type="search"
className="form-control bg-light border-start-0"
placeholder="Search your books..."
aria-label="Search books"
/>
</div>
</form>
{/* User Actions */}
<div className="d-flex align-items-center gap-3 ms-auto">
<button
className="btn btn-light rounded-circle position-relative"
type="button"
aria-label="Notifications"
>
<i className="bi bi-bell fs-5"></i>
<span className="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger">
3
</span>
</button>
<div className="d-flex align-items-center gap-2">
<div className="app-header__avatar bg-primary text-white rounded-circle d-flex align-items-center justify-content-center">
AB
</div>
<div className="d-none d-sm-block">
<div className="fw-semibold">An Bình</div>
<small className="text-secondary">
Book Lover
</small>
</div>
<i className="bi bi-chevron-down text-secondary"></i>
</div>
</div>
</div>
</nav>
</header>
)
}
export default Header
7.4. Giải thích import CSS
Ngay đầu file có:
import './Header.css'
Đây là cách import stylesheet của component trong React project sử dụng Vite.
Khi Vite xử lý module, CSS được import sẽ được đưa vào quá trình xử lý stylesheet của ứng dụng.
Như vậy, khi sử dụng Header, chúng ta không phải nhớ import Header.css thêm một lần nữa ở App.tsx.
Component tự quản lý stylesheet mà nó cần.
7.5. Giải thích export default
Cuối file có:
export default Header
Dòng này cho phép các file khác import Header Component.
Ví dụ:
import Header from './components/layout/Header'
Sau khi import, chúng ta sử dụng Header như một JSX element:
<Header />
Đây chính là cách React sử dụng component.
7.6. HTML và JSX khác nhau như thế nào?
Khi chuyển HTML sang JSX, cần chú ý một số quy tắc:
| HTML | JSX |
|---|---|
class | className |
for | htmlFor |
onclick | onClick |
tabindex | tabIndex |
style="width: 40px" | style={{ width: 40 }} |
Ví dụ về inline style:
HTML:
<div style="width: 40px; height: 40px;">
AB
</div>
JSX:
<div style={{ width: 40, height: 40 }}>
AB
</div>
JSX sử dụng JavaScript object cho thuộc tính style, vì vậy CSS properties được viết theo camelCase. Giá trị số thường được React hiểu là pixel đối với những thuộc tính có đơn vị độ dài.
8. Tạo Sidebar Component
Sau Header, chúng ta tiếp tục chuyển phần <aside> trong HTML Static sang React.
Tạo hai file:
src/components/layout/Sidebar.tsx
src/components/layout/Sidebar.css
Sidebar.css:
.app-sidebar {
min-height: calc(100vh - 72px);
}
.app-sidebar__link {
border-radius: 0.5rem;
}
.app-sidebar__link.active {
font-weight: 600;
}
Sidebar.tsx:
import './Sidebar.css'
function Sidebar() {
return (
<aside className="app-sidebar col-12 col-lg-2 bg-white border-end p-3">
<div className="text-uppercase small fw-bold text-secondary mb-3 px-2">
Library
</div>
<nav className="nav flex-column gap-2">
<a
href="#"
className="nav-link app-sidebar__link active bg-primary text-white"
>
<i className="bi bi-grid me-2"></i>
Dashboard
</a>
<a href="#" className="nav-link app-sidebar__link text-dark">
<i className="bi bi-book me-2"></i>
My Books
</a>
<a href="#" className="nav-link app-sidebar__link text-dark">
<i className="bi bi-bookmark me-2"></i>
Reading List
</a>
<a href="#" className="nav-link app-sidebar__link text-dark">
<i className="bi bi-tags me-2"></i>
Categories
</a>
<a href="#" className="nav-link app-sidebar__link text-dark">
<i className="bi bi-bar-chart me-2"></i>
Statistics
</a>
<a href="#" className="nav-link app-sidebar__link text-dark">
<i className="bi bi-gear me-2"></i>
Settings
</a>
</nav>
<hr className="my-4" />
<div className="bg-primary-subtle rounded-3 p-3">
<i className="bi bi-trophy text-primary fs-2"></i>
<h6 className="fw-bold mt-2">
Reading Goal
</h6>
<p className="small text-secondary mb-2">
You are doing great!
</p>
<div className="progress mb-2">
<div
className="progress-bar"
style={{ width: '60%' }}
/>
</div>
<small className="fw-semibold">
12 / 20 Books
</small>
</div>
</aside>
)
}
export default Sidebar
Ở đây, Sidebar cũng có stylesheet riêng. Điều này giúp mỗi component quản lý phần giao diện của mình thay vì tập trung toàn bộ CSS vào một file lớn.
9. Tạo DashboardPage Component
Tiếp theo là phần Main Content. Chúng ta sẽ tạo một page component để chứa các section của Dashboard.
Tạo:
src/pages/DashboardPage.tsx
src/pages/DashboardPage.css
Trong giai đoạn này, chưa cần tách toàn bộ section thành component nhỏ. Hãy chuyển cấu trúc Main Content từ HTML Static sang JSX trước.
DashboardPage.css:
.dashboard-page {
min-width: 0;
}
DashboardPage.tsx:
import './DashboardPage.css'
function DashboardPage() {
return (
<main className="dashboard-page col-12 col-lg-10 p-3 p-lg-4">
{/* Welcome Banner */}
<section className="bg-primary text-white rounded-4 p-4 mb-4">
<h1 className="fw-bold">
Welcome back, An Bình!
</h1>
<p className="mb-0">
Every book is a new journey.
What will you read today?
</p>
</section>
{/* Reading Statistics */}
<section className="mb-4">
<h2 className="h4 fw-bold mb-3">
Reading Overview
</h2>
<div className="row g-3">
<div className="col-12 col-md-4">
<article className="card border-0 shadow-sm h-100">
<div className="card-body">
<h3 className="h3 fw-bold">48</h3>
<p className="text-secondary mb-0">
Total Books
</p>
</div>
</article>
</div>
<div className="col-12 col-md-4">
<article className="card border-0 shadow-sm h-100">
<div className="card-body">
<h3 className="h3 fw-bold">12</h3>
<p className="text-secondary mb-0">
Completed Books
</p>
</div>
</article>
</div>
<div className="col-12 col-md-4">
<article className="card border-0 shadow-sm h-100">
<div className="card-body">
<h3 className="h3 fw-bold">5</h3>
<p className="text-secondary mb-0">
Currently Reading
</p>
</div>
</article>
</div>
</div>
</section>
{/* Other sections will be extracted into components */}
</main>
)
}
export default DashboardPage
Điểm quan trọng: DashboardPage là một page component. Nó đại diện cho nội dung của một trang, còn Header và Sidebar là những thành phần thuộc Application Layout.
10. Hiểu vai trò của index.html, main.tsx và App.tsx
Trước khi ghép các component, cần hiểu rõ ba file quan trọng trong React + Vite project.
10.1. index.html – HTML Entry Point
Đây là file HTML mà Vite sử dụng làm entry point của ứng dụng.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BookNest</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
Phần tử <div id="root"> là nơi React render ứng dụng.
10.2. main.tsx – Application Entry Point
File này chịu trách nhiệm khởi chạy React.
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap-icons/font/bootstrap-icons.css'
import './index.css'
import App from './App'
createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
</StrictMode>,
)
Giải thích:
bootstrap/dist/css/bootstrap.min.css: Import Bootstrap CSS từ node_modules.bootstrap-icons/font/bootstrap-icons.css: Import Bootstrap Icons.createRoot(): Tạo React root tại phần tử HTML có ID root.App: Component gốc của ứng dụng.StrictMode: Bật các kiểm tra bổ sung trong môi trường phát triển.
Lưu ý: Bootstrap CSS chỉ cần import một lần ở entry point. Các component khác sử dụng Bootstrap classes mà không cần import Bootstrap CSS thêm lần nữa.
10.3. App.tsx – Root Component
Đây là nơi chúng ta ghép các component cấp cao của ứng dụng.
import Header from './components/layout/Header'
import Sidebar from './components/layout/Sidebar'
import DashboardPage from './pages/DashboardPage'
import './App.css'
function App() {
return (
<div className="app-shell">
<Header />
<div className="container-fluid">
<div className="row">
<Sidebar />
<DashboardPage />
</div>
</div>
</div>
)
}
export default App
App.css:
.app-shell {
min-height: 100vh;
}
Hãy chú ý rằng App.tsx không chứa toàn bộ HTML của trang Dashboard.
Nó chỉ chịu trách nhiệm ghép:
App
├── Header
└── Application Body
├── Sidebar
└── DashboardPage
Đây là tư duy Component Composition: xây dựng giao diện lớn bằng cách kết hợp những component nhỏ hơn.
10.4. Luồng khởi chạy ứng dụng
11. Component Reusability và TypeScript Props
Sau khi có cấu trúc Header, Sidebar và DashboardPage, chúng ta tiếp tục phân tích những phần có thể tái sử dụng.
11.1. Nhận diện StatCard
Trong Reading Overview có ba Card:
| Card | Value | Label |
|---|---|---|
| 1 | 48 | Total Books |
| 2 | 12 | Completed Books |
| 3 | 5 | Currently Reading |
Cả ba Card đều có cùng cấu trúc:
StatCard
├── Icon
├── Value
└── Label
Chỉ có dữ liệu khác nhau. Vì vậy, thay vì viết ba đoạn JSX giống nhau, chúng ta tạo một component dùng chung.
11.2. Tạo StatCard Component
Tạo hai file:
src/components/dashboard/StatCard.tsx
src/components/dashboard/StatCard.css
StatCard.css:
.stat-card {
height: 100%;
}
.stat-card__icon {
flex-shrink: 0;
border-radius: 0.75rem;
}
StatCard.tsx:
import './StatCard.css'
type StatCardProps = {
icon: string
value: number
label: string
color: 'primary' | 'success' | 'warning'
}
function StatCard({
icon,
value,
label,
color
}: StatCardProps) {
return (
<article className="stat-card card border-0 shadow-sm">
<div className="card-body d-flex align-items-center gap-3">
<div className={`stat-card__icon bg-${color}-subtle text-${color} p-3`}>
<i className={`bi ${icon} fs-3`}></i>
</div>
<div>
<h3 className="h3 fw-bold mb-0">
{value}
</h3>
<p className="text-secondary mb-0">
{label}
</p>
</div>
</div>
</article>
)
}
export default StatCard
Ở đây có một khái niệm quan trọng: Props.
StatCardProps định nghĩa dữ liệu mà component nhận vào:
icon: Tên Bootstrap Icon.value: Giá trị thống kê.label: Nhãn hiển thị.color: Màu sắc của Card.
TypeScript giúp xác định kiểu dữ liệu và phát hiện lỗi khi truyền Props không đúng.
Ví dụ:
<StatCard
icon="bi-book"
value={48}
label="Total Books"
color="primary"
/>
React truyền những giá trị này vào component thông qua Props.
11.3. Sử dụng StatCard trong DashboardPage
Import component:
import StatCard from '../components/dashboard/StatCard'
Thay thế phần Statistics cũ bằng:
<section className="mb-4">
<h2 className="h4 fw-bold mb-3">
Reading Overview
</h2>
<div className="row g-3">
<div className="col-12 col-md-4">
<StatCard
icon="bi-book"
value={48}
label="Total Books"
color="primary"
/>
</div>
<div className="col-12 col-md-4">
<StatCard
icon="bi-check-circle"
value={12}
label="Completed Books"
color="success"
/>
</div>
<div className="col-12 col-md-4">
<StatCard
icon="bi-journal-bookmark"
value={5}
label="Currently Reading"
color="warning"
/>
</div>
</div>
</section>
Chúng ta chỉ cần viết StatCard một lần nhưng có thể sử dụng nhiều lần với những dữ liệu khác nhau.
Đây là một trong những lợi ích quan trọng của React Components.
11.4. Tạo BookCard Component (Currently Reading)
Tương tự StatCard, khu vực Currently Reading có hai thẻ sách lặp lại cùng cấu trúc, chỉ khác dữ liệu và màu sắc chủ đạo. Ta sẽ tách thành BookCard.
Tạo hai file:
src/components/dashboard/BookCard.tsx
src/components/dashboard/BookCard.css
BookCard.css:
.book-card {
height: 100%;
transition: transform 0.2s ease;
}
.book-card:hover {
transform: translateY(-2px);
}
.book-card__cover {
width: 88px;
flex-shrink: 0;
}
BookCard.tsx:
import './BookCard.css'
type BookCardProps = {
title: string
author: string
category: string
progress: number
color: 'primary' | 'success'
}
function BookCard({
title,
author,
category,
progress,
color
}: BookCardProps) {
return (
<article className="book-card card border-0 shadow-sm">
<div className="card-body">
<div className="d-flex gap-3">
<div className={`book-card__cover bg-${color}-subtle rounded-3 p-4 d-flex align-items-center justify-content-center`}>
<i className={`bi bi-journal-richtext display-5 text-${color}`}></i>
</div>
<div className="flex-grow-1">
<span className={`badge text-bg-${color} mb-2`}>
{category}
</span>
<h3 className="h5 fw-bold">{title}</h3>
<p className="text-secondary mb-2">{author}</p>
<div className="d-flex justify-content-between small mb-2">
<span>Reading Progress</span>
<strong>{progress}%</strong>
</div>
<div
className="progress"
role="progressbar"
aria-label={`${title} reading progress`}
aria-valuenow={progress}
aria-valuemin={0}
aria-valuemax={100}
>
<div
className={`progress-bar bg-${color}`}
style={{ width: `${progress}%` }}
/>
</div>
</div>
</div>
</div>
</article>
)
}
export default BookCard
11.5. Tạo RecentlyAddedItem và ReadingGoal Components
Để hoàn thiện các phần còn lại của Dashboard, ta tiếp tục tách item sách mới thêm (Recently Added) và thẻ mục tiêu đọc sách (Reading Goal).
Tạo file src/components/dashboard/RecentlyAddedItem.tsx (component này dùng thuần Bootstrap utilities nên chưa cần file CSS riêng):
type RecentlyAddedItemProps = {
title: string
author: string
color: 'warning' | 'info' | 'danger'
}
function RecentlyAddedItem({ title, author, color }: RecentlyAddedItemProps) {
return (
<div className="d-flex align-items-center gap-3">
<div className={`bg-${color}-subtle rounded-3 p-3`}>
<i className={`bi bi-book-half fs-2 text-${color}`}></i>
</div>
<div>
<h3 className="h6 fw-bold mb-1">{title}</h3>
<small className="text-secondary">{author}</small>
</div>
</div>
)
}
export default RecentlyAddedItem
Tiếp theo, tạo file src/components/dashboard/ReadingGoalCard.tsx:
type ReadingGoalCardProps = {
year: number
completed: number
target: number
}
function ReadingGoalCard({ year, completed, target }: ReadingGoalCardProps) {
const percentage = Math.round((completed / target) * 100)
return (
<section className="card border-0 shadow-sm">
<div className="card-body p-4">
<div className="d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-3">
<div>
<h2 className="h5 fw-bold">
<i className="bi bi-trophy text-warning me-2"></i>
Your {year} Reading Challenge
</h2>
<p className="text-secondary mb-0">
You have completed {completed} out of {target} books. Keep going!
</p>
</div>
<div className="text-md-end">
<div className="display-6 fw-bold text-primary">{percentage}%</div>
<small className="text-secondary">Annual Goal</small>
</div>
</div>
</div>
</section>
)
}
export default ReadingGoalCard
11.6. Hoàn thiện DashboardPage.tsx
Bây giờ, ghép toàn bộ các section (bao gồm cả Welcome Banner đầy đủ, Currently Reading, Recently Added và Reading Goal) vào src/pages/DashboardPage.tsx:
import StatCard from '../components/dashboard/StatCard'
import BookCard from '../components/dashboard/BookCard'
import RecentlyAddedItem from '../components/dashboard/RecentlyAddedItem'
import ReadingGoalCard from '../components/dashboard/ReadingGoalCard'
import './DashboardPage.css'
function DashboardPage() {
return (
<main className="dashboard-page col-12 col-lg-10 p-3 p-lg-4">
{/* Welcome Banner */}
<section className="bg-primary text-white rounded-4 p-4 mb-4">
<div className="row align-items-center">
<div className="col-md-8">
<p className="text-white-50 mb-2">
SUNDAY, NOVEMBER 15, 2026
</p>
<h1 className="fw-bold">
Welcome back, An Bình!
</h1>
<p className="mb-0">
Every book is a new journey. What will you read today?
</p>
</div>
<div className="col-md-4 text-md-end mt-3 mt-md-0">
<i className="bi bi-book-half display-1"></i>
</div>
</div>
</section>
{/* Reading Statistics */}
<section className="mb-4">
<h2 className="h4 fw-bold mb-3">Reading Overview</h2>
<div className="row g-3">
<div className="col-12 col-md-4">
<StatCard
icon="bi-book"
value={48}
label="Total Books"
color="primary"
/>
</div>
<div className="col-12 col-md-4">
<StatCard
icon="bi-check-circle"
value={12}
label="Completed Books"
color="success"
/>
</div>
<div className="col-12 col-md-4">
<StatCard
icon="bi-journal-bookmark"
value={5}
label="Currently Reading"
color="warning"
/>
</div>
</div>
</section>
{/* Currently Reading */}
<section className="mb-4">
<div className="d-flex justify-content-between align-items-center mb-3">
<h2 className="h4 fw-bold mb-0">Currently Reading</h2>
<a href="#" className="text-decoration-none">View All</a>
</div>
<div className="row g-3">
<div className="col-12 col-md-6">
<BookCard
title="Clean Code"
author="Robert C. Martin"
category="Technology"
progress={65}
color="primary"
/>
</div>
<div className="col-12 col-md-6">
<BookCard
title="Atomic Habits"
author="James Clear"
category="Personal Development"
progress={30}
color="success"
/>
</div>
</div>
</section>
{/* Recently Added */}
<section className="mb-4">
<div className="d-flex justify-content-between align-items-center mb-3">
<h2 className="h4 fw-bold mb-0">Recently Added</h2>
<a href="#" className="text-decoration-none">Browse Library</a>
</div>
<div className="card border-0 shadow-sm">
<div className="card-body">
<div className="row g-3">
<div className="col-12 col-md-4">
<RecentlyAddedItem
title="The Pragmatic Programmer"
author="Andrew Hunt"
color="warning"
/>
</div>
<div className="col-12 col-md-4">
<RecentlyAddedItem
title="Deep Work"
author="Cal Newport"
color="info"
/>
</div>
<div className="col-12 col-md-4">
<RecentlyAddedItem
title="Thinking, Fast and Slow"
author="Daniel Kahneman"
color="danger"
/>
</div>
</div>
</div>
</div>
</section>
{/* Reading Goal */}
<ReadingGoalCard year={2026} completed={12} target={20} />
</main>
)
}
export default DashboardPage
12. Hoàn thiện Component Tree
Sau khi phân rã các khu vực, cấu trúc ứng dụng có thể được hình dung như sau:
Luồng tổ chức giao diện:
- App quản lý cấu trúc tổng thể.
- Header và Sidebar thuộc Layout.
- DashboardPage đại diện cho một trang cụ thể.
- Các Section tổ chức nội dung trong trang.
- StatCard và BookCard là những component có thể tái sử dụng.
13. Chạy và Build ứng dụng
Sau khi chuyển các component vào React, chạy development server:
yarn dev
Mở địa chỉ Vite cung cấp và so sánh với file HTML Static.
Cần chú ý:
- Header hiển thị đúng.
- Sidebar nằm bên trái trên Desktop.
- Main Content nằm bên phải.
- Statistics Cards hiển thị đúng.
- Bootstrap Icons hoạt động.
- Responsive hoạt động khi thay đổi kích thước trình duyệt.
Kiểm tra production build:
yarn build
Nếu build thành công, Vite tạo thư mục dist chứa các file phục vụ triển khai.
Có thể chạy thử production build bằng:
yarn preview
14. Kết luận
Qua bài thực hành BookNest, chúng ta đã đi từ một trang HTML Static hoàn chỉnh đến cách tổ chức giao diện bằng React Components.
Điều quan trọng nhất là hiểu mối quan hệ giữa HTML, Bootstrap và React:
- HTML mô tả cấu trúc giao diện.
- Bootstrap điều khiển layout và cách hiển thị.
- React Components giúp phân chia giao diện thành những phần độc lập.
- CSS riêng giúp quản lý style của từng component.
- TypeScript Props giúp component nhận dữ liệu có kiểu rõ ràng.
- Component Composition giúp kết hợp các component thành một ứng dụng hoàn chỉnh.
Nguyên tắc cần ghi nhớ: Đừng cố biến mọi đoạn HTML thành một React Component. Hãy phân rã giao diện dựa trên trách nhiệm của từng khu vực, khả năng tái sử dụng và sự độc lập trong quản lý.
Quy trình HTML First → Component Analysis → React Conversion là một cách tiếp cận thực tế để xây dựng giao diện, đặc biệt khi bạn muốn hiểu rõ Bootstrap và React thay vì chỉ viết JSX theo mẫu có sẵn.
Nhận xét