React Wrapper for noUiSlider
nouislider-react is a React component wrapper for the popular noUiSlider library, providing a declarative way to integrate highly customizable range sliders into React applications. The package is currently at version 3.4.2 and aims to be a well-maintained alternative to other React wrappers for noUiSlider, explicitly stating its purpose to address maintenance issues found in similar packages. It maintains a regular, though not strictly scheduled, release cadence, primarily focusing on bug fixes, dependency updates, and minor feature enhancements. Key differentiators include direct exposure of all underlying noUiSlider options, support for additional React-specific properties like `clickablePips`, and comprehensive TypeScript type definitions, making it suitable for modern React and TypeScript projects. Developers should consult the original noUiSlider documentation for core slider configuration.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'set')
cause Attempting to access `noUiSlider` methods (like `set`, `get`) directly from a React `ref` object after version 3.3.0, which changed the ref handling.fixUse the `instanceRef` prop introduced in v3.3.0 to get the direct `noUiSlider` instance. For example, `instanceRef={instance => { this.sliderInstance = instance; }}` and then `this.sliderInstance.noUiSlider.set(...)`. -
Slider not appearing, or appearing unstyled and non-functional.
cause The required `nouislider.css` stylesheet has not been imported or the path is incorrect.fixAdd `import 'nouislider/distribute/nouislider.css';` to your application's entry point or the component where the slider is used. Ensure the `nouislider` package is correctly installed. -
TypeScript error: Property 'format' does not exist on type 'NouisliderProps' or 'Property 'onUpdate' does not exist on type 'NouisliderProps'.
cause Outdated TypeScript declarations or incorrect type usage, particularly regarding specific options or event handlers that were fixed in minor updates.fixUpdate `nouislider-react` to the latest version. For `format` issues, ensure you are on version 3.3.5 or later. For event handlers, check the types provided by the latest package version. -
Slider handles do not move, or events (onUpdate, onChange) are not firing correctly.
cause Issues with event handler binding or internal slider initialization, which were addressed in patch releases.fixUpgrade `nouislider-react` to version 3.4.0 (or newer) to benefit from fixes to event handler registration. For issues with initial setup, ensure `nouislider-react@3.4.2` or later is used.
Warnings
- breaking The way to access the underlying noUiSlider instance via a `ref` changed significantly in version 3.3.0. Direct `ref` on the component no longer works; instead, you must use the `instanceRef` prop.
- gotcha It is crucial to import the `nouislider.css` stylesheet for the slider to render correctly and be functional. Forgetting this import will result in an unstyled or visually broken slider.
- gotcha Many issues arise from not fully understanding the core `noUiSlider` library's options and event handling. The `nouislider-react` component directly passes most props to the underlying library.
- gotcha Version 3.4.2 introduced a fix for scenarios where the slider might be initialized multiple times, leading to unexpected behavior or errors. If you're encountering issues with slider re-initialization or updates after component re-renders, ensure you are on this version or newer.
- deprecated Version 3.3.4 was deprecated shortly after its release due to a bug fix for the `format` option that was not fully resolved, leading to version 3.3.5.
Install
-
npm install nouislider-react -
yarn add nouislider-react -
pnpm add nouislider-react
Imports
- Nouislider
import { Nouislider } from 'nouislider-react';import Nouislider from 'nouislider-react';
- nouislider.css
import 'nouislider-react/dist/nouislider.css';
import 'nouislider/distribute/nouislider.css';
- NouisliderProps
import type { NouisliderProps } from 'nouislider-react';
Quickstart
import React from 'react';
import Nouislider from 'nouislider-react';
import 'nouislider/distribute/nouislider.css';
const MySlider = () => (
<div style={{ padding: '30px' }}>
<Nouislider
range={{ min: 0, max: 100 }}
start={[20, 80]}
connect
tooltips
onUpdate={(values, handle, unencoded, tap, positions) => {
console.log(`Slider values: ${values}`);
}}
/>
</div>
);
export default MySlider;