typescript 51 lines · 7 steps

How a virtualized list renders in React

A windowing component that renders only the rows in view, keeping large lists fast by drawing a handful of DOM nodes at a time.

Explained by highlit
1import { useCallback, useRef, useState } from "react";
2 
3interface VirtualListProps<T> {
4 items: T[];
5 rowHeight: number;
6 height: number;
7 overscan?: number;
8 renderRow: (item: T, index: number) => React.ReactNode;
9}
10 
11export function VirtualList<T>({
12 items,
13 rowHeight,
14 height,
15 overscan = 3,
16 renderRow,
17}: VirtualListProps<T>) {
18 const [scrollTop, setScrollTop] = useState(0);
19 const rafRef = useRef<number | null>(null);
20 
21 const onScroll = useCallback((e: React.UIEvent<HTMLDivElement>) => {
22 const next = e.currentTarget.scrollTop;
23 if (rafRef.current !== null) cancelAnimationFrame(rafRef.current);
24 rafRef.current = requestAnimationFrame(() => setScrollTop(next));
25 }, []);
26 
27 const totalHeight = items.length * rowHeight;
28 const startIndex = Math.max(0, Math.floor(scrollTop / rowHeight) - overscan);
29 const visibleCount = Math.ceil(height / rowHeight) + overscan * 2;
30 const endIndex = Math.min(items.length, startIndex + visibleCount);
31 const offsetY = startIndex * rowHeight;
32 
33 const visible = items.slice(startIndex, endIndex);
34 
35 return (
36 <div
37 onScroll={onScroll}
38 style={{ height, overflowY: "auto", position: "relative" }}
39 >
40 <div style={{ height: totalHeight }}>
41 <div style={{ transform: `translateY(${offsetY}px)` }}>
42 {visible.map((item, i) => (
43 <div key={startIndex + i} style={{ height: rowHeight }}>
44 {renderRow(item, startIndex + i)}
45 </div>
46 ))}
47 </div>
48 </div>
49 </div>
50 );
51}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Rendering only the visible slice keeps DOM node count constant regardless of list length.
  2. 2A full-height spacer plus a translated inner container preserves the scrollbar while shifting rows into place.
  3. 3Throttling scroll updates through requestAnimationFrame avoids re-rendering faster than the browser can paint.

Related explainers

typescript
import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';
import localeFrExtra from '@angular/common/locales/extra/fr';
import localeDe from '@angular/common/locales/de';

Locale-aware bootstrapping in Angular

i18n localization dependency-injection
Intermediate 8 steps
typescript
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import * as Joi from 'joi';
 

Validating env config at boot in NestJS

configuration schema-validation environment-variables
Intermediate 8 steps
typescript
import { Inject, Injectable, Logger } from '@nestjs/common';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
import { InjectRepository } from '@nestjs/typeorm';

A cache-aside country lookup in NestJS

cache-aside dependency-injection batch-lookup
Intermediate 8 steps
typescript
import { Injectable, effect, signal, computed } from '@angular/core';
 
interface Preferences {
  theme: 'light' | 'dark';

A signal-based preferences store in Angular

signals state-management persistence
Intermediate 7 steps
typescript
import { useEffect, useState } from "react";
 
interface Section {
  id: string;

Building a scroll-spy hook in React

custom-hooks intersectionobserver dom-observation
Intermediate 8 steps
typescript
import { Injectable, Scope, Inject, NotFoundException } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { Request } from 'express';
import { DataSource } from 'typeorm';

Per-tenant database connections in NestJS

multi-tenancy connection-pooling dependency-injection
Advanced 8 steps

Share this explainer

Here's the card — post it anywhere.

How a virtualized list renders in React — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code