tannerlinsley / react-virtual
- понедельник, 11 мая 2020 г. в 00:21:56
JavaScript
⚛️ Hooks for virtualizing scrollable elements in React
Hooks for virtualizing scrollable elements in React
Enjoy this library? Try them all! React Table, React Query, React Form, React Charts
This library is being built and maintained by me, @tannerlinsley and I am always in need of more support to keep projects like this afloat. If you would like to get premium support, add your logo or name on this README, or simply just contribute to my open source Sponsorship goal, visit my Github Sponsors page!
|
Become a Sponsor! |
|
Become a Sponsor! |
|
|
Become a Sponsor! |
|
Become a Sponsor! |
|
Become a Supporter! |
|
Become a Fan! |
$ npm i --save react-virtual
# or
$ yarn add react-virtualReact Virtual uses Scarf to collect anonymized installation analytics. These analytics help support the maintainers of this library. However, if you'd like to opt out, you can do so by setting
scarfSettings.enabled = falsein your project'spackage.json. Alternatively, you can set the environment variableSCARF_ANALYTICS=falsebefore you install.
React Virtual's most distinguishing feature is that it's just one single custom hook instead of a set of components. In more trendy terms, this means that it is "headless", allowing you to control 100% all of the markup and styles, exactly how you want.
React Virtual supplies you with primitive utilities that allow you to build any range of virtualized experiences. One testament to this is that you can combine 2 instances of useVirtual onto the same markup to achieve a virtualized grid, where with other utilites, you would need to use a dedicated Grid-like component.
This is just a quick sample of what it looks like to use React Virtual. Please refer to the examples for more usage patterns.
function RowVirtualizerFixed() {
const parentRef = React.useRef()
const rowVirtualizer = useVirtual({
size: 10000,
parentRef,
estimateSize: React.useCallback(() => 35, []),
})
return (
<>
<div
ref={parentRef}
className="List"
style={{
height: `150px`,
width: `300px`,
overflow: 'auto',
}}
>
<div
className="ListInner"
style={{
height: `${rowVirtualizer.totalSize}px`,
width: '100%',
position: 'relative',
}}
>
{rowVirtualizer.virtualItems.map(virtualRow => (
<div
key={virtualRow.index}
className={virtualRow.index % 2 ? 'ListItemOdd' : 'ListItemEven'}
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: `${virtualRow.size}px`,
transform: `translateY(${virtualRow.start}px)`,
}}
>
Row {virtualRow.index}
</div>
))}
</div>
</div>
</>
)
}useVirtualconst {
virtualItems: [
{ index, start, size, end, measureRef },
/* ... */
],
totalSize,
scrollToIndex,
scrollToOffset,
} = useVirtual({
size,
parentRef,
estimateSize,
overscan,
horizontal,
scrollToFn
})size: Integer
parentRef: React.useRef(DOMElement)
estimateSize: Function(index) => Integer
React.useCallback()overscan: Integer
1horizontal: Boolean
falsetrue, this virtualizer will use width and scrollLeft instead of height and scrollTop to determine size and offset of virtualized items.scrollToFn: Function(offset) => void 0
virtualItems: Array<item>
item: Object
index: Integer
start: Integer
size: Integer
end: Integer
measureRef: React.useRef | Function(el: DOMElement) => void 0
totalSize: Integer
scrollToIndex: Function(index: Integer, { align: String }) => void 0
align: 'start' | 'center' | 'end' | 'auto'
autostart places the item at the top/left of the visible scroll areacenter places the item in the center of the visible scroll areaend places the item at the bottom/right of the visible scroll areaauto brings the item into the visible scroll area either at the start or end, depending on which is closer. If the item is already in view, it is placed at the top/left of the visible scroll area.scrollToOffset: Function(offsetInPixels: Integer, { align: String }) => void 0
align: 'start' | 'center' | 'end' | 'auto'
startstart places the offset at the top/left of the visible scroll areacenter places the offset in the center of the visible scroll areaend places the offset at the bottom/right of the visible scroll areaauto brings the offset into the visible scroll area either at the start or end, depending on which is closer. If the offset is already in view, it is placed at the top/left of the visible scroll area.Thanks goes to these wonderful people (emoji key):
Tanner Linsley |
This project follows the all-contributors specification. Contributions of any kind welcome!