React Perfect Scrollbar
react-perfect-scrollbar is a React component that wraps the 'perfect-scrollbar' library, providing a highly customizable and performant scrollbar solution for web applications. The current stable version is 1.5.8. Releases appear to be less frequent than monthly, but consistent enough to indicate active maintenance, with recent updates addressing bug fixes, new features like `onSync`, and improved TypeScript definitions. Its primary differentiator is offering a polished, native-feeling scroll experience that avoids the limitations and inconsistencies of default browser scrollbars, especially useful in complex UIs where standard scrollbars might be visually jarring or functionally inadequate. It achieves this by taking control of scroll behavior and rendering custom scrollbars that blend seamlessly with application design, while still relying on the underlying perfect-scrollbar library for the core functionality.
Common errors
-
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
cause Incorrect import of the `PerfectScrollbar` component, often due to using `require()` or incorrect named vs. default import.fixUse `import PerfectScrollbar from 'react-perfect-scrollbar';` to correctly import the default export. -
Scrollbar not visible or not updating when content changes.
cause The underlying perfect-scrollbar instance needs to be told to recalculate its dimensions after content changes.fixObtain a ref to the `PerfectScrollbar` component and call `ref.current.updateScroll()` after content changes, or implement custom sync logic using the `onSync` prop. -
Warning: Received `option` as a prop on `PerfectScrollbar`. This is not supported. If you meant to pass this to the underlying perfect-scrollbar instance, use the `options` prop instead.
cause Using the deprecated `option` prop instead of `options` for perfect-scrollbar configuration.fixRename the prop from `option` to `options`. -
Module not found: Can't resolve 'react-perfect-scrollbar/dist/css/styles.css' in '...' or Styles appear broken/missing.
cause The CSS file for `perfect-scrollbar` is not being imported or processed by the build system.fixAdd `import 'react-perfect-scrollbar/dist/css/styles.css';` to your main application file (e.g., `App.js` or `index.js`). Verify your bundler (e.g., Webpack, Vite) has a loader configured for CSS files.
Warnings
- breaking The `option` prop for perfect-scrollbar initialization parameters was deprecated and renamed to `options`. While `option` might still work if `options` is not provided, it's advised to update to `options` for future compatibility.
- breaking The underlying `perfect-scrollbar` library was updated to version 1.4.0, which introduced certain behavior changes. Developers should review the `perfect-scrollbar` release notes for version 1.4.0 to understand any potential impact on custom configurations or expected scroll behavior.
- gotcha When the content within PerfectScrollbar changes dynamically (e.g., via AJAX, state updates), the scrollbar might not automatically recalculate its size and position, leading to incorrect scrollbar appearance or functionality.
- gotcha Omitting the import of the required CSS file (`react-perfect-scrollbar/dist/css/styles.css`) will result in unstyled or non-functional scrollbars, as the component relies on these styles for its visual presentation.
Install
-
npm install react-perfect-scrollbar -
yarn add react-perfect-scrollbar -
pnpm add react-perfect-scrollbar
Imports
- PerfectScrollbar
const PerfectScrollbar = require('react-perfect-scrollbar');import PerfectScrollbar from 'react-perfect-scrollbar';
- CSS Styles
require('react-perfect-scrollbar/dist/css/styles.css');import 'react-perfect-scrollbar/dist/css/styles.css';
- PerfectScrollbarProps
import { PerfectScrollbarProps } from 'react-perfect-scrollbar';import type { PerfectScrollbarProps } from 'react-perfect-scrollbar';
Quickstart
import React, { useRef, useEffect } from 'react';
import PerfectScrollbar from 'react-perfect-scrollbar';
import 'react-perfect-scrollbar/dist/css/styles.css';
function MyScrollableComponent() {
const scrollRef = useRef(null);
useEffect(() => {
// Example: Scroll to bottom on mount
if (scrollRef.current) {
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
}
}, []);
return (
<div style={{ width: '300px', height: '200px', border: '1px solid #ccc' }}>
<PerfectScrollbar
containerRef={ref => (scrollRef.current = ref)}
options={{ suppressScrollX: false }}
onScrollY={container => console.log(`Scrolled Y: ${container.scrollTop}`)}
style={{ maxHeight: '100%', maxWidth: '100%' }}
>
<div style={{ height: '500px', padding: '10px' }}>
{Array.from({ length: 50 }).map((_, i) => (
<p key={i}>Item {i + 1}: This is some scrollable content.</p>
))}
</div>
</PerfectScrollbar>
</div>
);
}
// To render this:
// ReactDOM.render(<MyScrollableComponent />, document.getElementById('root'));