React Custom Scrollbars Component (Fork)
react-custom-scrollbars-2 is a community-maintained fork of the original `react-custom-scrollbars` package, providing a customizable React component for creating custom scrollbars while leveraging native browser scrolling behavior. It aims to address bug fixes and maintain active development where the original project became stale. The current stable version is 4.5.0. Key differentiators include frictionless native scrolling, mobile device native scrollbar support, extensive customization options (e.g., auto-hide, auto-height), universal rendering (SSR compatible), and performance optimizations using `requestAnimationFrame` for 60fps. It avoids extra stylesheets and ships with 100% code coverage. This package is ideal for applications requiring fine-grained control over scrollbar appearance and behavior without sacrificing performance or accessibility.
Common errors
-
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.
cause Incorrect import of `Scrollbars` component, often due to a default import instead of a named import.fixChange `import Scrollbars from 'react-custom-scrollbars-2';` to `import { Scrollbars } from 'react-custom-scrollbars-2';` -
TypeError: Cannot read properties of undefined (reading 'current')
cause Often occurs when trying to access `this.refs.scrollbars.getScrollHeight()` or similar methods, indicating `this.refs` is not properly set up or the component is not yet mounted.fixEnsure the component is fully mounted before attempting to access its instance methods. For functional components, use `useRef` and ensure the ref is attached to the `Scrollbars` component and has been assigned. -
Scrollbars are not appearing or styled correctly despite applying custom render props.
cause Commonly, the custom `renderView`, `renderTrack`, or `renderThumb` components do not correctly apply the style and props provided to them by the `Scrollbars` component.fixEnsure your custom render functions correctly spread `props` (e.g., `<div {...props} />`) and apply `style` when provided, as these are crucial for positioning and visibility.
Warnings
- gotcha This package is a fork of the original `react-custom-scrollbars`. While it aims for bug fixes and active maintenance, be aware of the lineage when migrating from the original package, as specific behavioral changes or new bugs might exist that differ from the original's last stable release.
- gotcha When using `autoHeight`, ensure `autoHeightMin` and `autoHeightMax` are set appropriately to prevent layout shifts or incorrect sizing, especially if the content height can vary wildly. Default values might not always suit your specific use case.
- gotcha Custom rendering components (`renderView`, `renderThumbHorizontal`, etc.) must pass `ref` to their children if they internally use `ref` to manage scrollbar elements. Failure to do so can break internal calculations or event handling.
Install
-
npm install react-custom-scrollbars-2 -
yarn add react-custom-scrollbars-2 -
pnpm add react-custom-scrollbars-2
Imports
- Scrollbars
const { Scrollbars } = require('react-custom-scrollbars-2');import { Scrollbars } from 'react-custom-scrollbars-2'; - ScrollbarProps
import type { ScrollbarProps } from 'react-custom-scrollbars-2'; - Scrollbars (as default)
import Scrollbars from 'react-custom-scrollbars-2';
import Scrollbars from 'react-custom-scrollbars-2/lib/Scrollbars';
Quickstart
import React, { Component } from 'react';
import { Scrollbars } from 'react-custom-scrollbars-2';
class App extends Component {
render() {
return (
<div style={{ padding: '20px', border: '1px solid #ccc', maxWidth: '600px', margin: '50px auto' }}>
<h1>My Content with Custom Scrollbars</h1>
<Scrollbars
style={{ width: 500, height: 300, border: '1px solid #eee' }}
autoHide
autoHideTimeout={1000}
autoHideDuration={200}
>
<p>Some great content that will overflow the container. This paragraph and subsequent content are here to demonstrate the scrolling functionality. </p>
<p>You can customize the appearance and behavior of the scrollbars, such as making them automatically hide when not in use. This helps maintain a clean UI.</p>
<p>More content to fill the space and ensure scrolling is necessary. Keep adding text until the scrollbar appears, allowing you to test the component effectively.</p>
<p>Final paragraph for testing purposes. Enjoy your custom scrollbars!</p>
<p>This is even more content to make sure the scrollbar is definitely there.</p>
<p>And one last line.</p>
</Scrollbars>
</div>
);
}
}
// For a real app, you would render this component:
// ReactDOM.render(<App />, document.getElementById('root'));
export default App;