React Swipeable Views Utilities
react-swipeable-views-utils provides Higher-Order Components (HOCs) and utilities designed to extend the functionality of `react-swipeable-views`. This package offers features like automatic slide progression (`autoPlay`), virtualized rendering for performance with many slides (`virtualize`), and keyboard navigation (`bindKeyboard`). The current stable version is `0.14.1`. Historically, releases have been somewhat irregular, with the last significant update over a year ago, suggesting a maintenance rather than active development phase. Its primary differentiator is its tight integration with `react-swipeable-views`, providing common advanced behaviors out-of-the-box without requiring complex custom implementations. It relies on React's HOC pattern to inject enhanced behavior into a base `SwipeableViews` component.
Common errors
-
Error: `SwipeableViews` does not support `componentWillReceiveProps` or `componentWillMount` on React 16.3+.
cause Using `react-swipeable-views-utils` versions `>=0.14.0-alpha.0` with React < 16.3, or an older `react-swipeable-views` with a newer React.fixEnsure both `react` and `react-swipeable-views` are at `^16.3.0` or higher, and `react-swipeable-views-utils` is at `0.14.0` or later. -
TypeError: Cannot read properties of undefined (reading 'scrollTo')
cause This error can occur if `scrollTop` is not handled correctly in some environments, or if a view portal is used incorrectly, preventing proper DOM manipulation.fixUpgrade to `0.14.0` or later, which includes fixes for `scrollTop` and issues with portals inside views. Ensure your `SwipeableViews` component is rendered in a standard DOM flow. -
Warning: Accessing PropTypes via the main 'react' package is deprecated. Use the 'prop-types' package instead.
cause Older versions of `react-swipeable-views` or its utilities might still rely on `React.PropTypes` directly.fixEnsure `react-swipeable-views` and `react-swipeable-views-utils` are on their latest versions (`0.14.x`) which should address `prop-types` usage correctly.
Warnings
- breaking The package removed legacy context, which might affect applications relying on older React context APIs. Ensure your `react-swipeable-views` setup does not use deprecated context features.
- breaking Version `0.14.0-alpha.0` introduced a minimum React version requirement of `16.3` due to its use of `UNSAFE_*` lifecycle methods (`componentWillReceiveProps`, `componentWillMount`) to workaround console warnings. Using older React versions will lead to errors or warnings.
- gotcha When using the `autoPlay` HOC, the `onChangeIndex` prop's third parameter was previously swallowed in certain cases. This was fixed, but ensure your `onChangeIndex` callback correctly handles its arguments if you experienced unexpected behavior.
- gotcha For mouse events to work with `react-swipeable-views` (and thus with these utilities), you might need to explicitly add the `enableMouseEvents` prop to your `SwipeableViews` component. This is not always intuitive for users expecting touch and mouse parity.
- gotcha The `autoplay` HOC pauses when the window is hidden. While this is often desired to save resources, it can be a 'gotcha' if you expect continuous autoplay even when the tab is in the background.
Install
-
npm install react-swipeable-views-utils -
yarn add react-swipeable-views-utils -
pnpm add react-swipeable-views-utils
Imports
- autoPlay
const autoPlay = require('react-swipeable-views-utils').autoPlay;import { autoPlay } from 'react-swipeable-views-utils'; - virtualize
import virtualize from 'react-swipeable-views-utils/lib/virtualize';
import { virtualize } from 'react-swipeable-views-utils'; - bindKeyboard
import { BindKeyboard } from 'react-swipeable-views-utils';import { bindKeyboard } from 'react-swipeable-views-utils';
Quickstart
import * as React from 'react';
import SwipeableViews from 'react-swipeable-views';
import { autoPlay } from 'react-swipeable-views-utils';
const AutoPlaySwipeableViews = autoPlay(SwipeableViews);
const DemoCarousel = () => {
const [index, setIndex] = React.useState(0);
const handleChangeIndex = (idx) => {
setIndex(idx);
};
return (
<div style={{ maxWidth: 400, margin: 'auto' }}>
<h2>Auto-Playing Carousel</h2>
<AutoPlaySwipeableViews
index={index}
onChangeIndex={handleChangeIndex}
enableMouseEvents
interval={3000}
>
<div style={{ background: '#f44336', height: 200, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
<h3>Slide 1</h3>
</div>
<div style={{ background: '#2196f3', height: 200, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
<h3>Slide 2</h3>
</div>
<div style={{ background: '#4caf50', height: 200, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
<h3>Slide 3</h3>
</div>
</AutoPlaySwipeableViews>
<div style={{ textAlign: 'center', marginTop: 10 }}>
{['●', '●', '●'].map((dot, i) => (
<span
key={i}
style={{
cursor: 'pointer',
margin: '0 5px',
color: i === index ? '#333' : '#bbb',
fontSize: '24px'
}}
onClick={() => setIndex(i)}
>
{dot}
</span>
))}
</div>
</div>
);
};
export default DemoCarousel;