Skip to content
Source
Sign inSign up
TanStack/virtual

Add use-case driven examples #1097

Opentannerlinsleywants to merge 1 commit intomain fromclaude/use-case-driven-examples-zgdZGopened

tannerlinsleycommented

Add three new React examples that focus on common use cases rather than implementation details. Each example defaults to dynamic sizing but includes interactive controls to switch between fixed, variable, and dynamic modes, helping users understand when to use each approach.

  • List: Vertical list virtualization with mode switching
  • Horizontal List: Horizontal list/carousel virtualization
  • Grid: 2D grid virtualization with both row and column virtualization

🎯 Changes

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).
    1. ⚠️ No Changeset found

      Latest commit: 079cf8f4fdb0c176a27e92bd732fae580f8be179

      Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

      This PR includes no changesets

      When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

      Click here to learn what changesets are, and how to add one.

      Click here if you're a maintainer who wants to add a changeset to this PR

    2. piecyk reviewed

      @tannerlinsley Looks sweet 👍

      Let’s update the pnpm-lock.yaml, adjust getItemKey to reflect sizingMode, and remove the redundant examples, then we should be good to go.

      @@ -0,0 +97,21 @@
      97+ <strong>Fixed mode:</strong> All items have the same fixed height (35px).
      98+ Best performance, use when all items are identical height.
      99+ </>
      100+ )}
      101+ {sizingMode === 'variable' && (
      102+ <>
      103+ <strong>Variable mode:</strong> Each item has a known but different height.
      104+ Heights are pre-computed, not measured at runtime.
      105+ </>
      106+ )}
      107+ </p>
      108+
      109+ <div
      110+ ref={parentRef}
      111+ className="List"
      112+ style={{
      113+ height: 400,
      114+ width: '100%',
      115+ maxWidth: 600,
      116+ overflowY: 'auto',
      117+ contain: 'strict',
      piecykcommented

      Let's include overflowAnchor as it's critical for virtualizers to prevent browser scroll anchoring from interfering with scroll position management, especially in dynamic sizing mode where elements are measured at runtime.

      Suggested change
      113- height: 400,
      114- width: '100%',
      115- maxWidth: 600,
      116- overflowY: 'auto',
      117- contain: 'strict',
      113+ height: 400,
      114+ width: '100%',
      115+ maxWidth: 600,
      116+ overflowY: 'auto',
      117+ contain: 'strict',
      118+ overflowAnchor: 'none',
      @@ -0,0 +99,21 @@
      99+ Best performance, use when all items are identical width.
      100+ </>
      101+ )}
      102+ {sizingMode === 'variable' && (
      103+ <>
      104+ <strong>Variable mode:</strong> Each item has a known but different width.
      105+ Widths are pre-computed, not measured at runtime.
      106+ </>
      107+ )}
      108+ </p>
      109+
      110+ <div
      111+ ref={parentRef}
      112+ className="List"
      113+ style={{
      114+ height: 150,
      115+ width: '100%',
      116+ maxWidth: 800,
      117+ overflowX: 'auto',
      118+ overflowY: 'hidden',
      119+ contain: 'strict',
      piecykcommented

      Same as in vertical list

      Suggested change
      114- height: 150,
      115- width: '100%',
      116- maxWidth: 800,
      117- overflowX: 'auto',
      118- overflowY: 'hidden',
      119- contain: 'strict',
      114+ height: 150,
      115+ width: '100%',
      116+ maxWidth: 800,
      117+ overflowX: 'auto',
      118+ overflowY: 'hidden',
      119+ contain: 'strict',
      120+ overflowAnchor: 'none',
      @@ -0,0 +139,21 @@
      139+ <strong>Fixed mode:</strong> All cells have the same dimensions
      140+ (35px height, 100px width). Best performance for uniform grids.
      141+ </>
      142+ )}
      143+ {sizingMode === 'variable' && (
      144+ <>
      145+ <strong>Variable mode:</strong> Each row/column has pre-computed
      146+ dimensions. Use when sizes are known but vary per row/column.
      147+ </>
      148+ )}
      149+ </p>
      150+
      151+ <div
      152+ ref={parentRef}
      153+ className="List"
      154+ style={{
      155+ height: 500,
      156+ width: '100%',
      157+ maxWidth: 800,
      158+ overflow: 'auto',
      159+ contain: 'strict',
      piecykcommented

      Same as in vertical list

      Suggested change
      155- height: 500,
      156- width: '100%',
      157- maxWidth: 800,
      158- overflow: 'auto',
      159- contain: 'strict',
      155+ height: 500,
      156+ width: '100%',
      157+ maxWidth: 800,
      158+ overflow: 'auto',
      159+ contain: 'strict',
      160+ overflowAnchor: 'none',
      @@ -0,0 +54,21 @@
      54+ overscan: 5,
      55+ })
      56+
      57+ // Column virtualizer
      58+ const columnVirtualizer = useVirtualizer({
      59+ horizontal: true,
      60+ count: columnCount,
      61+ getScrollElement: () => parentRef.current,
      62+ estimateSize: React.useCallback(
      63+ (index: number) => {
      64+ switch (sizingMode) {
      65+ case 'fixed':
      66+ return 100
      67+ case 'variable':
      68+ return variableColumnWidths[index]
      69+ case 'dynamic':
      70+ default:
      71+ return 120
      72+ }
      73+ },
      74+ [sizingMode],
      piecykcommented

      Wrapping estimateSize in React.useCallback with sizingMode as a dependency doesn’t automatically update the virtualizer’s measurement cache, that’s by design.

      There isn’t currently an explicit way to invalidate the virtualizer cache. A common workaround is to change getItemKey

      Suggested change
      62- estimateSize: React.useCallback(
      63- (index: number) => {
      64- switch (sizingMode) {
      65- case 'fixed':
      66- return 100
      67- case 'variable':
      68- return variableColumnWidths[index]
      69- case 'dynamic':
      70- default:
      71- return 120
      72- }
      73- },
      74- [sizingMode],
      62+ estimateSize: (index: number) => {
      63+ switch (sizingMode) {
      64+ case 'fixed':
      65+ return 100
      66+ case 'variable':
      67+ return variableColumnWidths[index]
      68+ case 'dynamic':
      69+ default:
      70+ return 120
      71+ }
      72+ },
      73+ getItemKey: React.useCallback((index: number) => index, [sizingMode]),

Sign in to join this conversation.