Recipe List with Keys trong ReactJS
Trong loạt bài tập này về ReactJS: Lists và Keys, mình sẽ thực hiện Bài 8 - "Recipe List with Keys nâng cao". Trong bài này, mình sẽ tạo một component RecipeListWithKeys để hiển thị một danh sách các công thức nấu ăn, với mỗi công thức có một key duy nhất.
Tạo một component Recipe List with Keys trong ReactJS
Mình sẽ bắt đầu bằng việc tạo một ứng dụng React mới và thêm component RecipeListWithKeys vào đó.
Bước 1: Tạo một ứng dụng React mới
npx create-react-app react-recipe-list cd react-recipe-list
Bước 2: Tạo component RecipeListWithKeys
Trong thư mục src, tạo một file mới có tên là RecipeListWithKeys.js. File này sẽ chứa component RecipeListWithKeys.
RecipeListWithKeys.js
import React from 'react';
const RecipeListWithKeys = () => {
const recipes = [
{ id: 1, name: 'Spaghetti Carbonara', ingredients: ['Spaghetti', 'Bacon', 'Eggs', 'Parmesan cheese', 'Black pepper'] },
{ id: 2, name: 'Chicken Alfredo', ingredients: ['Fettuccine pasta', 'Chicken breast', 'Heavy cream', 'Parmesan cheese', 'Garlic'] },
{ id: 3, name: 'Margherita Pizza', ingredients: ['Pizza dough', 'Tomato sauce', 'Mozzarella cheese', 'Fresh basil'] },
{ id: 4, name: 'Caesar Salad', ingredients: ['Romaine lettuce', 'Croutons', 'Parmesan cheese', 'Caesar dressing'] },
];
return (
<div>
<h2>Recipe List with Keys</h2>
<ul className="recipe-list">
{recipes.map(recipe => (
<li key={recipe.id}>
<h3>{recipe.name}</h3>
<ul>
{recipe.ingredients.map((ingredient, index) => (
<li key={index}>{ingredient}</li>
))}
</ul>
</li>
))}
</ul>
</div>
);
};
export default RecipeListWithKeys;
Bước 3: Tạo file CSS cho RecipeListWithKeys
Tạo một file CSS mới có tên RecipeListWithKeys.css trong thư mục src.
RecipeListWithKeys.css
.recipe-list {
list-style: none;
padding: 0;
}
.recipe-list li {
margin-bottom: 20px;
}
.recipe-list li h3 {
margin-bottom: 10px;
}
.recipe-list li ul {
list-style: none;
padding: 0;
}
.recipe-list li ul li {
margin-left: 20px;
}
Bước 4: Sử dụng component RecipeListWithKeys trong App
Sửa nội dung file src/App.js để sử dụng component RecipeListWithKeys.
App.js
import React from 'react';
import RecipeListWithKeys from './RecipeListWithKeys';
import './RecipeListWithKeys.css';
function App() {
return (
<div className="App">
<h1>React Recipe List with Keys Exercise</h1>
<RecipeListWithKeys />
</div>
);
}
export default App;
Bước 5: Chạy ứng dụng React
Quay lại terminal và chạy lệnh sau để khởi động ứng dụng React:
npm start
Mở trình duyệt và truy cập http://localhost:3000 để xem ứng dụng của bạn. Bạn sẽ thấy một danh sách các công thức nấu ăn được hiển thị, với mỗi công thức có một key duy nhất.

Kết bài:
Với bài tập "Recipe List with Keys" này, chúng ta đã tạo một danh sách các công thức nấu ăn trong React, với mỗi công thức được gán một key duy nhất. Điều này giúp React hiểu và quản lý danh sách hiệu quả hơn. Hãy tiếp tục khám phá và thực hành để làm quen với các tính năng khác của ReactJS và phát triển kỹ năng lập trình của bạn!
Bài giải
-------------------- ######## --------------------
Câu hỏi thường gặp liên quan:
- Simple List with Keys trong ReactJS
- Todo List with Keys trong ReactJS
- Product List with Keys trong ReactJS
- User List with Keys trong ReactJS
- Blog Post List with Keys trong ReactJS
- Comment List with Keys trong ReactJS
- Image Gallery with Keys trong ReactJS
- Recipe List with Keys trong ReactJS
- Contact List with Keys trong ReactJS
- Event List with Keys trong ReactJS
- Movie List with Keys trong ReactJS

Các hàm xử lý chuỗi trong Javascript (cắt / tách / nối chuỗi ..)
Chia lấy phần dư / chia lấy phần nguyên trong javascript
Các cách khai báo biến trong Javascript
Các sự kiện (Event) trong Javascript
Hướng tạo thanh search bar bằng CSS
Hàm array.slice() trong Javascript
Tính tổng hai số bằng Javascript (cộng hai số)
Cách khai báo biến trong PHP, các loại biến thường gặp
Download và cài đặt Vertrigo Server
Thẻ li trong HTML
Thẻ article trong HTML5
Cấu trúc HTML5: Cách tạo template HTML5 đầu tiên
Cách dùng thẻ img trong HTML và các thuộc tính của img
Thẻ a trong HTML và các thuộc tính của thẻ a thường dùng