Virtual scrolling for large lists with react-window

Maya Patel Jan 2026
1 tab
import { FixedSizeList as List } from 'react-window'
import AutoSizer from 'react-virtualized-auto-sizer'
import { Post } from '@/types'
import { PostCard } from './PostCard'

interface VirtualizedPostListProps {
  posts: Post[]
}

export function VirtualizedPostList({ posts }: VirtualizedPostListProps) {
  const Row = ({ index, style }: { index: number; style: React.CSSProperties }) => (
    <div style={style}>
      <PostCard post={posts[index]} />
    </div>
  )

  return (
    <AutoSizer>
      {({ height, width }) => (
        <List
          height={height}
          itemCount={posts.length}
          itemSize={250} // Fixed height per item
          width={width}
        >
          {Row}
        </List>
      )}
    </AutoSizer>
  )
}
1 file · typescript Explain with highlit

Rendering thousands of list items kills performance. Virtual scrolling renders only visible items plus a buffer, dramatically reducing DOM nodes. The react-window library provides FixedSizeList and VariableSizeList components that handle viewport calculations. I wrap list items in the virtualizer's child component and provide item height. For variable heights, I estimate initial heights and measure actual heights on render. Virtual scrolling works well with React Query's infinite queries—fetch pages on demand as users scroll. The trade-off is losing browser find-in-page and accessibility features, so I only virtualize truly large lists. For most cases, pagination or infinite scroll is simpler.