React Responsive Carousel
React Responsive Carousel is a powerful and highly customizable carousel component for React applications, currently at version 3.2.23. It offers responsive design, mobile-friendly swipe gestures, server-side rendering compatibility, and keyboard navigation. Developers can customize animation duration, enable auto-play with custom intervals, and configure infinite loops in both horizontal and vertical directions. A key differentiator is its flexibility, supporting various content types like images, videos, and text as direct children, along with extensive customization options for thumbs, arrows, indicators, status displays, and animation handlers. However, the package is currently in a maintenance-only state, as the primary maintainer lacks time for active development. New features are not being added, though safe pull requests might be merged occasionally, implying an infrequent release cadence focused primarily on stability rather than feature expansion.
Common errors
-
Carousel is not defined
cause The Carousel component was not imported or was imported incorrectly.fixEnsure you have `import { Carousel } from 'react-responsive-carousel';` at the top of your file. -
Module not found: Error: Can't resolve 'react-responsive-carousel/lib/styles/carousel.min.css'
cause The path to the CSS file is incorrect, or your build tool (e.g., Webpack, Vite) is not configured to process CSS imports from `node_modules`.fixVerify the import path `import "react-responsive-carousel/lib/styles/carousel.min.css";`. If using a bundler, ensure you have appropriate loaders (e.g., `css-loader`, `style-loader` for Webpack) configured in your build setup. -
TypeError: Cannot read properties of undefined (reading 'length') when rendering Carousel.
cause Often occurs when children passed to `Carousel` are not valid React elements or are `undefined`/`null`, leading to rendering issues.fixEnsure that all direct children passed to the `Carousel` component are valid React elements (e.g., `<div>`, `<img>`, custom components) and that there are no `null` or `undefined` values in the children array if dynamically rendered.
Warnings
- gotcha The package is in maintenance mode. The primary maintainer does not have time for active development, meaning new features are unlikely, and critical bug fixes may have delayed timelines. Consider forking or contributing if you require active development.
- breaking Version 3.0.2 introduced significant changes, including adding support for free children, vertical carousels, free legends, and updating ref callbacks. Callback names were also renamed, potentially breaking existing implementations.
- breaking Version 2.0.0 updated React to 0.14.2 and officially dropped support for Internet Explorer 8 (IE8).
- gotcha The carousel styles are not automatically included and must be imported explicitly in your application. Forgetting to do so will result in an unstyled and potentially non-functional carousel.
Install
-
npm install react-responsive-carousel -
yarn add react-responsive-carousel -
pnpm add react-responsive-carousel
Imports
- Carousel
const Carousel = require('react-responsive-carousel').Carousel;import { Carousel } from 'react-responsive-carousel'; - styles
require('react-responsive-carousel/lib/styles/carousel.min.css');import "react-responsive-carousel/lib/styles/carousel.min.css";
- CarouselProps
import type { CarouselProps } from 'react-responsive-carousel';
Quickstart
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import "react-responsive-carousel/lib/styles/carousel.min.css"; // mandatory styles
import { Carousel } from 'react-responsive-carousel';
class DemoCarousel extends Component {
render() {
return (
<Carousel>
<div>
<img src="https://via.placeholder.com/600x400?text=Image+1" alt="Legend 1" />
<p className="legend">Legend 1</p>
</div>
<div>
<img src="https://via.placeholder.com/600x400?text=Image+2" alt="Legend 2" />
<p className="legend">Legend 2</p>
</div>
<div>
<img src="https://via.placeholder.com/600x400?text=Image+3" alt="Legend 3" />
<p className="legend">Legend 3</p>
</div>
</Carousel>
);
}
}
// Assuming a div with class 'demo-carousel' exists in your HTML
ReactDOM.render(<DemoCarousel />, document.querySelector('.demo-carousel'));