Tạo một component ImageCarousel với state bằng ReactJS
Trong bài tập này, mình sẽ tạo một component Image Carousel trong ReactJS sử dụng state và lifecycle. Image Carousel là một phần quen thuộc trong nhiều ứng dụng web, cho phép người dùng duyệt qua nhiều hình ảnh một cách thuận tiện. Mình sẽ sử dụng useState để theo dõi vị trí hiện tại của hình ảnh và sử dụng useEffect để xử lý việc lặp lại quá trình khi đến hình ảnh cuối cùng.
Image Carousel trong ReactJS
File và cấu trúc dự án
Hãy bắt đầu bằng việc tạo các file và thư mục cần thiết cho dự án của mình:

ImageCarousel.js - Component Image Carousel
// src/components/ImageCarousel.js
import React, { useState } from 'react';
import './ImageCarousel.css';
const ImageCarousel = () => {
const [currentImageIndex, setCurrentImageIndex] = useState(0);
const images = [
'https://example.com/image1.jpg',
'https://example.com/image2.jpg',
'https://example.com/image3.jpg',
// Thêm đường dẫn URL hình ảnh khác nếu cần
];
const handleNextImage = () => {
setCurrentImageIndex((prevIndex) => (prevIndex + 1) % images.length);
};
const currentImageUrl = images[currentImageIndex];
return (
<div className="image-carousel">
<img src={currentImageUrl} alt={`Ảnh ${currentImageIndex + 1}`} />
<button onClick={handleNextImage}>Tiếp Theo</button>
</div>
);
};
export default ImageCarousel;
ImageCarousel.css - CSS cho Image Carousel
/* src/components/ImageCarousel.css */
.image-carousel {
text-align: center;
margin-top: 20px;
}
img {
max-width: 100%;
border: 2px solid #ddd;
border-radius: 8px;
margin-bottom: 15px;
}
button {
padding: 10px;
font-size: 16px;
cursor: pointer;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
}
button:hover {
background-color: #0056b3;
}
App.js - File chính của ứng dụng
// src/App.js
import React from 'react';
import ImageCarousel from './components/ImageCarousel';
import './App.css';
function App() {
return (
<div className="App">
<header className="App-header">
<ImageCarousel />
</header>
</div>
);
}
export default App;
Kết quả:


Với bài tập này, mình đã tạo thành công một Image Carousel sử dụng ReactJS, một cách tuyệt vời để hiển thị và duyệt qua nhiều hình ảnh một cách dễ dàng. Bạn có thể tùy chỉnh nó bằng cách thêm hoặc thay đổi các đường dẫn URL của hình ảnh theo nhu cầu của ứng dụng của bạn. Chúc mừng bạn đã hoàn thành bài tập!
Bài giải
-------------------- ######## --------------------
Câu hỏi thường gặp liên quan:
- Tạo một component Counter có chứa một state là count bằng ReactJS
- Tạo một component Clock có chứa state là currentTime trong ReactJS
- Tạo một component Toggle với một state là isOn trong ReactJS
- Tạo một component TodoList với state là một mảng todos trong ReactJS
- Tạo một component CountdownTimer với state trong ReactJS
- Tạo một component ColorPicker có chứa state trong ReactJS
- Tạo một component WeatherApp với state trong ReactJS
- Tạo một component QuizGame với state bằng ReactJS
- Tạo một component ImageCarousel với state bằng ReactJS
- Tạo một component ShoppingCart với state bằng 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