Click chuột phải vào file HTML → "Open with Live Server". Browser sẽ tự reload khi bạn lưu file (Ctrl+S). Cực kỳ tiện khi làm việc với AI — generate code, save, thấy kết quả ngay.
2
Bài 6.2 — JavaScript Tương Tác
DOM Manipulation Patterns
javascript
// === CHỌN ELEMENTS ===
const btn = document.querySelector('#my-button'); // 1 element
const items = document.querySelectorAll('.item'); // NodeList
const input = document.getElementById('search-input'); // By ID
// === THÊM/XÓA CLASS ===
btn.classList.add('active');
btn.classList.remove('active');
btn.classList.toggle('active'); // Add nếu không có, remove nếu có
// === TẠO ELEMENT ĐỘNG ===
function createCard(item) {
const card = document.createElement('div');
card.className = 'card';
card.innerHTML = `
${item.title}
${item.description}
`;
return card;
}
// === EVENT DELEGATION (hiệu quả hơn addEventListener trên nhiều items) ===
document.querySelector('#list').addEventListener('click', (e) => {
const deleteBtn = e.target.closest('.btn-delete');
if (deleteBtn) {
const id = deleteBtn.dataset.id;
deleteItem(id);
}
});
// === FETCH API — gọi API ===
async function fetchTodos() {
try {
const response = await fetch('https://jsonplaceholder.typicode.com/todos?_limit=10');
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
const todos = await response.json();
renderTodos(todos);
} catch (error) {
showError('Không thể tải dữ liệu: ' + error.message);
}
}
function renderTodos(todos) {
const container = document.querySelector('#todos-container');
container.innerHTML = todos.map(todo => `
${todo.title}
`).join('');
}
3
Bài 6.3 — React với AI
Setup React với Vite (nhanh nhất hiện tại)
bash
# Tạo React app với Vite
npm create vite@latest my-react-app -- --template react
cd my-react-app
npm install
npm run dev
# → Mở browser: http://localhost:5173
Component Pattern với AI
Khi dùng AI để tạo React components, luôn cung cấp:
text — Prompt mẫu cho React component
Tôi đang dùng React 18 với Vite. Viết component ProductCard:
Props: { id, name, price, image, rating, onAddToCart }
- Hiển thị ảnh sản phẩm, tên, giá (format VND), rating (stars)
- Button "Thêm vào giỏ" gọi onAddToCart(id)
- Loading state khi đang xử lý
- Dùng CSS modules (ProductCard.module.css)
- PropTypes validation
- Responsive: full width trên mobile, 1/3 trên desktop
jsx — ProductCard.jsx (AI generated + reviewed)
import { useState } from 'react';
import PropTypes from 'prop-types';
import styles from './ProductCard.module.css';
function StarRating({ rating }) {
return (
Dùng Tailwind CSS (v3) viết component Notification Toast:
- 4 variants: success (green), error (red), warning (yellow), info (blue)
- Mỗi toast có: icon, title, message, close button
- Animation: slide in từ phải, tự đóng sau 5 giây
- Stack nhiều toasts theo chiều dọc, góc dưới phải màn hình
- React component, nhận prop: { type, title, message, onClose }
Cài extension Tailwind CSS IntelliSense trong VS Code. Khi gõ className, autocomplete hiện danh sách classes kèm preview. Copilot + IntelliSense = không cần nhớ bất kỳ class nào!
5
Bài 6.5 — Dự Án Thực Hành: Xây Dựng React Task Manager App
Chúng ta sẽ xây dựng một ứng dụng Task Manager bằng React + Tailwind CSS — có thêm/xóa/sửa task, lọc theo trạng thái, lưu localStorage, và responsive layout. Toàn bộ UI được viết với sự hỗ trợ của Copilot.
Bước 1 — Tạo Dự Án React Với Vite
bash — Tạo project Vite + React
# Tạo project mới với Vite (nhanh hơn Create React App 10x)
npm create vite@latest task-manager -- --template react
cd task-manager
# Cài dependencies mặc định
npm install
# Cài Tailwind CSS
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
# Cài thư viện bổ sung
npm install lucide-react uuid
# Chạy development server
npm run dev
# Mở trình duyệt tại: http://localhost:5173
bash — Build production và deploy lên Netlify/Vercel
# Build cho production
npm run build
# Output: thư mục dist/ chứa file tĩnh
# Preview bản build local
npm run preview
# Deploy lên Netlify (cách đơn giản nhất)
# 1. Cài Netlify CLI
npm install -g netlify-cli
# 2. Đăng nhập Netlify
netlify login
# 3. Deploy
netlify deploy --prod --dir=dist
# Nhận URL dạng: https://your-app.netlify.app
# Hoặc deploy lên Vercel
npm install -g vercel
vercel --prod
💡 Mẹo từ ThanhDoIT
AI rất giỏi Tailwind nhưng hay tạo ra class string quá dài. Dùng cn() helper (clsx + tailwind-merge) để merge classes conditionally — sạch và maintainable hơn.
Khi ask AI tạo React component, yêu cầu luôn: "Thêm loading state, error state và empty state cho component này." 3 states này hay bị quên.
useEffect có dependency array rỗng [] ≠ "chạy một lần". Nó chạy sau mỗi lần component mount. Hiểu rõ React lifecycle trước khi dùng AI generate hooks.
Bật React StrictMode (default trong Vite). Nó sẽ mount/unmount component 2 lần trong dev để phát hiện bugs — một số code AI generate sẽ fail test này.
6
Bài 6.6 — Design System & Component Library với AI
Thay vì code từng component riêng lẻ, senior developer build Design System — một bộ components tái sử dụng với style nhất quán. AI giúp bạn tạo Design System trong vài giờ thay vì vài tuần.
Chỉ dùng khi: computation thực sự expensive (lọc mảng lớn, sort, format phức tạp) hoặc function được pass vào component đã memo hóa
Không cần: tính toán đơn giản, string/number operations — overhead của memo lớn hơn lợi ích
AI thường thêm useMemo/useCallback vào mọi chỗ — hỏi lại: "Optimization này có thực sự cần không với data size này?"
Tôi có React component sau đang bị lag khi render danh sách 500+ items.
Hãy phân tích và optimize:
[paste component code vào đây]
Yêu cầu:
1. Identify các re-render không cần thiết
2. Thêm React.memo cho child components phù hợp
3. Thêm useMemo cho expensive computations
4. Thêm useCallback cho event handlers được pass xuống
5. Nếu cần, suggest dùng virtual list (react-window) cho list quá dài
6. Giải thích tại sao mỗi optimization cần thiết
Paste code thực tế + yêu cầu giải thích → Copilot không chỉ fix mà còn dạy bạn tại sao.
🎯 Thực Hành: Build Mini Design System
Tạo file design-tokens.css với color palette, spacing, typography của bạn
Hỏi Copilot: "Generate Button component React với 4 variants dùng CSS variables này"
Build thêm Input component với states: default, focus, error, disabled
Build Card component: header, body, footer slots
Tạo Modal component với backdrop, close button, keyboard ESC support
Export tất cả từ src/components/index.ts — có thể import như: import { Button, Input, Card } from '@/components'