React Responsive Carousel is a simple and responsive carousel component for React applications. It provides an easy way to create image carousels that automatically adapt to different screen sizes, making it perfect for building mobile-friendly image galleries. This guide walks through setting up and creating responsive carousels using React Responsive Carousel with React, from installation to a working implementation. This is part 61 of a series on using React Responsive Carousel with React.
Prerequisites
Before you begin, make sure you have:
- Node.js version 14.0 or higher installed
- npm, yarn, or pnpm package manager
- A React project (version 16.8 or higher) or create-react-app setup
- Basic knowledge of React components
- Familiarity with JavaScript/TypeScript
Installation
Install React Responsive Carousel using your preferred package manager:
npm install react-responsive-carousel
Or with yarn:
yarn add react-responsive-carousel
Or with pnpm:
pnpm add react-responsive-carousel
After installation, your package.json should include:
{
"dependencies": {
"react-responsive-carousel": "^3.2.0",
"react": "^18.0.0",
"react-dom": "^18.0.0"
}
}
Project Setup
Import the CSS file in your main entry file:
// src/index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import 'react-responsive-carousel/lib/styles/carousel.min.css';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
First Example / Basic Usage
Let's create a simple image carousel. Create a new file src/CarouselExample.jsx:
// src/CarouselExample.jsx
import React from 'react';
import { Carousel } from 'react-responsive-carousel';
import 'react-responsive-carousel/lib/styles/carousel.min.css';
function CarouselExample() {
const images = [
'https://via.placeholder.com/800x400/007bff/ffffff?text=Slide+1',
'https://via.placeholder.com/800x400/28a745/ffffff?text=Slide+2',
'https://via.placeholder.com/800x400/ffc107/000000?text=Slide+3',
'https://via.placeholder.com/800x400/dc3545/ffffff?text=Slide+4'
];
return (
<div style={{ padding: '20px', maxWidth: '800px', margin: '0 auto' }}>
<h2>Basic Carousel Example</h2>
<Carousel>
{images.map((image, index) => (
<div key={index}>
<img src={image} alt={`Slide ${index + 1}`} />
<p className="legend">Slide {index + 1}</p>
</div>
))}
</Carousel>
</div>
);
}
export default CarouselExample;
Update your App.jsx:
// src/App.jsx
import React from 'react';
import CarouselExample from './CarouselExample';
import './App.css';
function App() {
return (
<div className="App">
<CarouselExample />
</div>
);
}
export default App;
This creates a basic responsive carousel with navigation and legends.
Understanding the Basics
React Responsive Carousel provides several key features:
- Carousel component: Main carousel container
- Automatic responsiveness: Adapts to screen size automatically
- Navigation arrows: Previous/next buttons
- Indicators: Dots for slide navigation
- Touch support: Swipe gestures on mobile
- Legend support: Text captions for slides
Key concepts:
-
Slide structure: Each slide is a
divwith content inside -
Legend: Optional text caption using
legendclass - Responsive: Automatically adjusts to container width
- Touch gestures: Swipe support enabled by default
- Styling: Import CSS file for default styles
Here's an example with custom configuration:
// src/AdvancedCarouselExample.jsx
import React from 'react';
import { Carousel } from 'react-responsive-carousel';
import 'react-responsive-carousel/lib/styles/carousel.min.css';
function AdvancedCarouselExample() {
const images = [
{
url: 'https://via.placeholder.com/800x400/007bff/ffffff?text=Slide+1',
legend: 'First Slide'
},
{
url: 'https://via.placeholder.com/800x400/28a745/ffffff?text=Slide+2',
legend: 'Second Slide'
},
{
url: 'https://via.placeholder.com/800x400/ffc107/000000?text=Slide+3',
legend: 'Third Slide'
},
{
url: 'https://via.placeholder.com/800x400/dc3545/ffffff?text=Slide+4',
legend: 'Fourth Slide'
}
];
return (
<div style={{ padding: '20px', maxWidth: '800px', margin: '0 auto' }}>
<h2>Advanced Carousel Example</h2>
<Carousel
showArrows={true}
showStatus={true}
showIndicators={true}
showThumbs={false}
infiniteLoop={true}
autoPlay={true}
interval={3000}
transitionTime={500}
>
{images.map((image, index) => (
<div key={index}>
<img src={image.url} alt={image.legend} />
<p className="legend">{image.legend}</p>
</div>
))}
</Carousel>
</div>
);
}
export default AdvancedCarouselExample;
Practical Example / Building Something Real
Let's build a complete image gallery carousel:
// src/ImageGalleryCarousel.jsx
import React, { useState } from 'react';
import { Carousel } from 'react-responsive-carousel';
import 'react-responsive-carousel/lib/styles/carousel.min.css';
function ImageGalleryCarousel() {
const [selectedIndex, setSelectedIndex] = useState(0);
const images = [
{
url: 'https://via.placeholder.com/800x500/007bff/ffffff?text=Image+1',
title: 'Image 1',
description: 'Description for image 1'
},
{
url: 'https://via.placeholder.com/800x500/28a745/ffffff?text=Image+2',
title: 'Image 2',
description: 'Description for image 2'
},
{
url: 'https://via.placeholder.com/800x500/ffc107/000000?text=Image+3',
title: 'Image 3',
description: 'Description for image 3'
},
{
url: 'https://via.placeholder.com/800x500/dc3545/ffffff?text=Image+4',
title: 'Image 4',
description: 'Description for image 4'
},
{
url: 'https://via.placeholder.com/800x500/6c757d/ffffff?text=Image+5',
title: 'Image 5',
description: 'Description for image 5'
}
];
const handleChange = (index) => {
setSelectedIndex(index);
};
return (
<div style={{ padding: '20px', maxWidth: '900px', margin: '0 auto' }}>
<h1>Image Gallery Carousel</h1>
<Carousel
showArrows={true}
showStatus={true}
showIndicators={true}
infiniteLoop={true}
selectedItem={selectedIndex}
onChange={handleChange}
autoPlay={false}
transitionTime={500}
>
{images.map((image, index) => (
<div key={index}>
<img src={image.url} alt={image.title} />
<div className="legend" style={{
backgroundColor: 'rgba(0,0,0,0.7)',
padding: '15px',
textAlign: 'left'
}}>
<h3 style={{ margin: '0 0 5px 0', color: 'white' }}>{image.title}</h3>
<p style={{ margin: 0, color: 'white' }}>{image.description}</p>
</div>
</div>
))}
</Carousel>
<div style={{ marginTop: '20px', textAlign: 'center', color: '#666' }}>
Image {selectedIndex + 1} of {images.length}
</div>
</div>
);
}
export default ImageGalleryCarousel;
Now create a product carousel:
// src/ProductCarousel.jsx
import React from 'react';
import { Carousel } from 'react-responsive-carousel';
import 'react-responsive-carousel/lib/styles/carousel.min.css';
function ProductCarousel() {
const products = [
{
image: 'https://via.placeholder.com/400x400/007bff/ffffff?text=Product+1',
name: 'Product 1',
price: '$99'
},
{
image: 'https://via.placeholder.com/400x400/28a745/ffffff?text=Product+2',
name: 'Product 2',
price: '$149'
},
{
image: 'https://via.placeholder.com/400x400/ffc107/000000?text=Product+3',
name: 'Product 3',
price: '$199'
},
{
image: 'https://via.placeholder.com/400x400/dc3545/ffffff?text=Product+4',
name: 'Product 4',
price: '$249'
}
];
return (
<div style={{ padding: '20px', maxWidth: '600px', margin: '0 auto' }}>
<h2>Product Carousel</h2>
<Carousel
showArrows={true}
showIndicators={true}
infiniteLoop={true}
centerMode={true}
centerSlidePercentage={80}
showThumbs={false}
>
{products.map((product, index) => (
<div key={index} style={{ padding: '10px' }}>
<img src={product.image} alt={product.name} />
<div className="legend" style={{
backgroundColor: 'white',
color: '#333',
padding: '15px'
}}>
<h3 style={{ margin: '0 0 5px 0' }}>{product.name}</h3>
<p style={{ margin: 0, fontWeight: 'bold', color: '#007bff' }}>{product.price}</p>
</div>
</div>
))}
</Carousel>
</div>
);
}
export default ProductCarousel;
Update your App.jsx:
// src/App.jsx
import React from 'react';
import ImageGalleryCarousel from './ImageGalleryCarousel';
import ProductCarousel from './ProductCarousel';
import './App.css';
function App() {
return (
<div className="App">
<ImageGalleryCarousel />
<ProductCarousel />
</div>
);
}
export default App;
This example demonstrates:
- Image gallery carousel with navigation
- Product carousel with center mode
- Custom legends and captions
- State management for selected slide
- Responsive design
- Touch/swipe support
Common Issues / Troubleshooting
Carousel not displaying: Make sure you've imported the CSS file (
import 'react-responsive-carousel/lib/styles/carousel.min.css'). The carousel requires styles to render correctly.Images not showing: Ensure image URLs are valid and accessible. Check that images are properly wrapped in
divelements inside the Carousel.Navigation not working: Check that
showArrowsandshowIndicatorsprops are set totrueif you want navigation controls.AutoPlay not working: Set
autoPlay={true}and specify anintervalin milliseconds. Also ensureinfiniteLoopis enabled for continuous playback.Responsive issues: The carousel is responsive by default. If it's not adapting, check that the container has proper width constraints and isn't fixed to a specific size.
Touch gestures not working: Touch support is enabled by default. If gestures aren't working, check for CSS conflicts or ensure the carousel container is properly sized.
Next Steps
Now that you have a basic understanding of React Responsive Carousel:
- Learn about advanced carousel configurations
- Explore custom styling options
- Implement thumbnail navigation
- Add custom animations
- Create vertical carousels
- Learn about other carousel libraries (react-slick, swiper)
- Check the official repository: https://github.com/leandrowd/react-responsive-carousel
- Look for part 62 of this series for more advanced topics
Summary
You've successfully set up React Responsive Carousel in your React application and created image galleries and product carousels with navigation controls. React Responsive Carousel provides a simple, responsive solution for creating carousels that work well on all devices.
SEO Keywords
react-responsive-carousel
React carousel component
react-responsive-carousel tutorial
React image carousel
react-responsive-carousel installation
React responsive slider
react-responsive-carousel example
React mobile carousel
react-responsive-carousel setup
React touch carousel
react-responsive-carousel customization
React carousel library
react-responsive-carousel navigation
React image gallery
react-responsive-carousel getting started
Top comments (0)