Frontend Blocks
typescript
Advanced
Virtual List Component
Render only visible rows in a large list for smooth performance with thousands of items.
typescript
import { useRef, useState } from 'react';
export function VirtualList({ items, rowHeight = 48, visibleRows = 10, renderItem }) {
const [scrollTop, setScrollTop] = useState(0);
const startIdx = Math.floor(scrollTop / rowHeight);
const endIdx = Math.min(startIdx + visibleRows + 1, items.length);
return (
<div style={{ height: visibleRows * rowHeight, overflowY: 'auto' }}
onScroll={e => setScrollTop(e.currentTarget.scrollTop)}>
<div style={{ height: items.length * rowHeight, position: 'relative' }}>
{items.slice(startIdx, endIdx).map((item, i) => (
<div key={startIdx + i}
style={{ position: 'absolute', top: (startIdx + i) * rowHeight, height: rowHeight }}>
{renderItem(item, startIdx + i)}
</div>
))}
</div>
</div>
);
}
Prerequisites
- React >= 17
Pro Tips
- → Use react-window for production-grade virtual lists.
Commercial Licensing
Acquire a production access token or template license for enterprise deployment.
Module Info
- Language
- typescript
- Level
- Advanced
- Read time
- 6 min
- Published
- Nov 2024
- License
- Commercial License
Newsletter
New modules every week, directly to your inbox.
Similar Modules
Copy to Clipboard Button
React component that copies text to clipboard and shows a Copied! confirmation.
typescript
·
Beginner
Framer Motion Fade-In Wrapper
Reusable component that fades any child element in on mount.
typescript
·
Beginner
Compound Component Pattern
Implement a flexible Tabs component using React Context and compound component pattern.
typescript
·
Intermediate