React Slider Component
react-slider is an actively maintained React component designed for creating highly customizable range sliders. The current stable version is 2.0.6, with recent updates focusing on stability and compatibility across modern web environments. It supports both single and multiple handles, offers horizontal and vertical orientations, and includes accessibility features through props like `ariaLabel` and `ariaValuetext`. Key differentiators include built-in `ResizeObserver` support for dynamic slider resizing and a commitment to broad React compatibility, explicitly listing React 16, 17, and 18 as peer dependencies. The project demonstrates a consistent release cadence, regularly publishing patch updates for bug fixes and minor enhancements, with major versions introducing significant features and occasional breaking changes. It addresses common slider challenges such as mobile 'snap back' behavior and module resolution issues for modern JavaScript module systems.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'call')
cause This error specifically occurred in `react-slider@0.11.2` after a brief introduction of arrow functions that broke some older build environments or transpilation setups.fixIf you are specifically on `react-slider@0.11.2`, downgrade to `0.11.1` or upgrade to `0.11.3` (or higher) which reverted the problematic arrow functions to ES5 syntax for broader compatibility. -
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components)...Check the render method of `App`.
cause This common React error indicates that you are likely attempting to import `Slider` as a named export, but `react-slider` provides it as a default export.fixChange your import statement from `import { Slider } from 'react-slider';` to the correct default import: `import Slider from 'react-slider';` -
Invariant Violation: Min and Max values for `react-slider` must be numbers.
cause The `min`, `max`, `value`, or `defaultValue` props passed to the `Slider` component are not strictly numbers. This often happens if values are read from inputs as strings or are `undefined`.fixEnsure that all value-related props (`min`, `max`, `value`, `defaultValue`) are explicitly numeric types. Use `parseInt()`, `parseFloat()`, or the unary plus operator (`+`) to convert string inputs to numbers. -
Module not found: Can't resolve 'react-slider' in '...' OR Failed to compile: Can't resolve 'react-slider/dist/...' for CommonJS
cause These errors typically point to module resolution issues, especially in `react-slider` v2.x due to its dual package extensions and how bundlers resolve ESM vs CommonJS paths.fixVerify your bundler (Webpack, Rollup, Vite) configuration correctly handles ESM/CJS interoperability. For TypeScript, ensure `tsconfig.json` has `"moduleResolution": "bundler"` (TypeScript 5+) or `"node"`/`"nodenext"` (for older TS). Clear your `node_modules` and `package-lock.json`/`yarn.lock` and reinstall dependencies.
Warnings
- breaking Version 2.0.0 introduced a breaking change related to updating `create-react-styleguide` to v8.x. While primarily an internal build system dependency, this might affect custom development setups or build processes that rely on specific versions or outputs from the internal style guide tooling.
- gotcha `react-slider` versions 2.0.0 onwards leverage dual package extensions, which can sometimes lead to module resolution issues in older tooling or mixed CommonJS/ESM projects. Early v2.x patch releases specifically addressed 'module resolution' and 'dual package extensions' bugs.
- gotcha Prior to v2.0.5, users reported a 'snap back' issue where slider handles on mobile devices would occasionally revert to a previous position during interaction, creating a frustrating user experience.
- gotcha Older versions (pre-v0.11.2) had known issues with React ref compatibility for React 16+ and a bug where the slider value could incorrectly become `NaN`. These issues can lead to unexpected behavior or runtime errors.
Install
-
npm install react-slider -
yarn add react-slider -
pnpm add react-slider
Imports
- Slider
import { Slider } from 'react-slider';import Slider from 'react-slider';
- Slider (CommonJS)
const Slider = require('react-slider'); - TypeScript Type Definitions
import Slider from 'react-slider'; import type { SliderProps } from 'react-slider';
Quickstart
import React from 'react';
import ReactDOM from 'react-dom/client';
import Slider from 'react-slider';
// Basic inline styles for demonstration. In a real app, use CSS classes.
const thumbStyle = {
height: '25px',
width: '25px',
backgroundColor: '#555',
borderRadius: '50%',
cursor: 'grab',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
color: '#fff',
fontSize: '0.8em'
};
const trackStyle = (state, props) => ({
top: '0',
bottom: '0',
background: props.index === 1 ? '#ddd' : '#007bff',
borderRadius: '5px'
});
const sliderContainerStyle = {
width: '80%',
margin: '50px auto',
height: '25px',
background: '#eee',
borderRadius: '5px',
display: 'flex',
alignItems: 'center',
};
function App() {
const [value, setValue] = React.useState(50);
const [rangeValues, setRangeValues] = React.useState([25, 75]);
return (
<div style={{ fontFamily: 'sans-serif', textAlign: 'center' }}>
<h1>React Slider Examples</h1>
<h2>Single Handle Slider</h2>
<div style={sliderContainerStyle}>
<Slider
value={value}
onChange={setValue}
renderTrack={(props, state) => <div {...props} style={trackStyle(state, props)} />}
renderThumb={(props, state) => <div {...props} style={thumbStyle}>{state.valueNow}</div>}
min={0}
max={100}
step={1}
ariaLabel="Single value slider"
/>
</div>
<p>Current Value: {value}</p>
<h2>Range Slider</h2>
<div style={sliderContainerStyle}>
<Slider
value={rangeValues}
onChange={setRangeValues}
renderTrack={(props, state) => <div {...props} style={trackStyle(state, props)} />}
renderThumb={(props, state) => <div {...props} style={thumbStyle}>{state.valueNow}</div>}
min={0}
max={100}
step={1}
ariaLabel={['Min value slider', 'Max value slider']}
pearling
minDistance={10}
/>
</div>
<p>Current Range: [{rangeValues[0]}, {rangeValues[1]}]</p>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root') ?? document.createElement('div'));
root.render(<App />);