React Slick Carousel
react-slick is a popular React component that serves as a port of the original jQuery-based `slick-carousel` library. It provides a highly customizable and responsive carousel solution for React applications, abstracting away the underlying jQuery dependency for seamless integration. The current stable version is 0.31.0, with a cadence of regular patch and minor releases addressing bugs and supporting newer React versions. Key differentiators include its extensive feature set inherited from slick-carousel, broad compatibility with React versions (from 0.14.0 up to 19.0.0), and a strong community backing. It requires separate installation and explicit CSS imports from `slick-carousel` for its essential styling and fonts.
Common errors
-
Module not found: Can't resolve 'slick-carousel/slick/slick.css' in '...'
cause The `slick-carousel` package, which provides the necessary CSS files, has not been installed or is not correctly resolved by your bundler.fixInstall `slick-carousel` using `npm install slick-carousel` or `yarn add slick-carousel` and ensure your bundler is configured to handle CSS imports. -
TypeError: Cannot read properties of undefined (reading 'body') (or similar ReferenceError for 'window' or 'document') during server-side rendering.
cause `react-slick` attempts to access browser-specific `window` or `document` objects when rendered on the server, where these are unavailable.fixWrap the `Slider` component in a client-only rendering component or use dynamic imports to ensure it is only executed and rendered in the browser environment. -
Carousel appears unstyled, missing navigation arrows, dots, or responsive behavior is broken.
cause The essential `slick-carousel` CSS files (`slick.css` and `slick-theme.css`) are not correctly imported into your application or are being overridden by other styles.fixVerify that `import 'slick-carousel/slick/slick.css';` and `import 'slick-carousel/slick/slick-theme.css';` are present in your main application entry point or a component where the Slider is used, and check your browser's developer tools for conflicting styles. -
Invariant Violation: Invalid hook call. Hooks can only be called inside of the body of a function component.
cause This often indicates that multiple versions of React are being loaded in your application, or an incompatible React version is being used with the installed `react-slick` version.fixEnsure a single, compatible version of `react` and `react-dom` is installed and used throughout your project. For React 18+ projects, ensure `react-slick` is version `0.29.0` or higher.
Warnings
- gotcha Styling and fonts are not included with `react-slick` itself. You *must* install `slick-carousel` separately and import its CSS files for the slider to render correctly.
- breaking Earlier versions of `react-slick` (specifically pre-0.29.0) might not be fully compatible with React 18 and newer APIs, potentially leading to rendering issues or warnings.
- gotcha `slick-carousel`, the underlying library that `react-slick` wraps, has a jQuery dependency. While `react-slick` abstracts this for React users, be aware of the potential for increased bundle size or indirect dependency conflicts if jQuery is managed globally.
- gotcha When using `react-slick` in server-side rendering (SSR) environments, it might require specific handling or dynamic imports, as it relies on browser-specific APIs (e.g., `window`, `document`) during initialization, leading to `ReferenceError`.
Install
-
npm install react-slick -
yarn add react-slick -
pnpm add react-slick
Imports
- Slider
import { Slider } from 'react-slick';import Slider from 'react-slick';
- slick.css
import 'react-slick/slick/slick.css';
import 'slick-carousel/slick/slick.css';
- slick-theme.css
import 'react-slick/slick/slick-theme.css';
import 'slick-carousel/slick/slick-theme.css';
Quickstart
import React from 'react';
import Slider from 'react-slick';
import 'slick-carousel/slick/slick.css';
import 'slick-carousel/slick/slick-theme.css';
export default function SimpleSlider() {
const settings = {
dots: true,
infinite: true,
speed: 500,
slidesToShow: 1,
slidesToScroll: 1,
autoplay: true,
autoplaySpeed: 2000
};
return (
<div style={{ width: '80%', margin: '0 auto', padding: '20px 0' }}>
<h2> Simple Slider </h2>
<Slider {...settings}>
<div><h3>1</h3></div>
<div><h3>2</h3></div>
<div><h3>3</h3></div>
<div><h3>4</h3></div>
<div><h3>5</h3></div>
<div><h3>6</h3></div>
</Slider>
</div>
);
}