react-cool-virtual
A tiny React hook for rendering large datasets like a breeze.
Getting Started
To use React Cool Virtual, you must use react@16.8.0
or greater which includes hooks.
Installation
This package is distributed via npm.
⚠️ This package using ResizeObserver API under the hook. Most modern browsers support it natively, you can also add polyfill for full browser support.
CDN
If you're not using a module bundler or package manager. We also provide a UMD build which is available over the unpkg.com CDN. Simply use a <script>
tag to add it after React CND links as below:
Once you've added this you will have access to the window.ReactCoolVirtual.useVirtual
variable.
Basic Usage
Here's the basic concept of how it rocks:
✨ Pretty easy right? React Cool Virtual is more powerful than you think. Let's explore more use cases through the examples!
Examples
Some of the common use cases that React Cool Virtual can help you out.
Fixed Size
This example demonstrates how to create a fixed size row. For column or grid, please refer to CodeSandbox.
Variable Size
This example demonstrates how to create a variable size row. For column or grid, please refer to CodeSandbox.
Dynamic Size
This example demonstrates how to create a dynamic size row. For column or grid, please refer to CodeSandbox.
? Scrollbar thumb is jumping? It's because the total size of the items is gradually corrected along with an item has been measured. You can tweak the
itemSize
to reduce the phenomenon.
Real-time Resize
This example demonstrates how to create a real-time resize row (e.g. accordion, collapse etc.). For column or grid, please refer to CodeSandbox.
Responsive Web Design (RWD)
Coming soon...
Scroll to Offset/Items
You can imperatively scroll to offset or items as follows:
Smooth Scrolling
React Cool Virtual provides the smooth scrolling feature out of the box, all you need to do is turn the smooth
option on.
The default easing effect is easeInOutCubic, and the duration is 500 milliseconds. You can easily customize your own effect as follows:
? For more cool easing effects, please check it out.
Infinite Scroll
Coming soon...
Server-side Rendering (SSR)
Coming soon...
Performance Optimization
Coming soon...
How to Share A ref
?
You can share a ref
as follows, here we take the outerRef
as the example:
Working in TypeScript
React Cool Virtual is built with TypeScript, you can tell the hook what type of your outer and inner elements are as follows:
? For more available types, please check it out.
API
Coming soon...
ResizeObserver Polyfill
ResizeObserver has good support amongst browsers, but it's not universal. You'll need to use polyfill for browsers that don't support it. Polyfills is something you should do consciously at the application level. Therefore React Cool Virtual doesn't include it.
We recommend using @juggle/resize-observer:
Then pollute the window
object:
You could use dynamic imports to only load the file when the polyfill is required:
To Do...
- [ ] Unit testing
- [ ] Reverse scrolling
- [ ] Infinite loop
- [ ]
scrollBy
method