Skip to content
Source
Sign inSign up
TanStack/virtual

feat(react-virtual): add useVirtualizerSnapshot for React Compiler compatibility via useSyncExternalStore #1241

Openkklem0(GitHub profile, opens in a new tab)wants to merge 4 commits intomain fromrefs/pull/1241/headopened

Showing 11 changed files with 106 additions and 0 deletions (loading 7).

x
0 of 11 viewed, 11 left
5 additions and 0 deletions.changeset/hungry-donkeys-applaud.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/react-virtual': minor
+---
+​
+Add `useVirtualizerSnapshot` and `useWindowVirtualizerSnapshot`: variants of the existing hooks that return `virtualItems` / `totalSize` as immutable snapshot data via `useSyncExternalStore`, with the stable `virtualizer` instance alongside for imperative APIs. Components consuming the snapshot hooks are compatible with React Compiler — the compiler skips components calling `useVirtualizer` as a known-incompatible API (#736, #743, #1119), while snapshot consumers compile and memoize correctly.
100 additions and 0 deletionsdocs/framework/react/react-virtual.mdModified
View file
@@ -33,6 +33,106 @@
​
This function returns a window-based `Virtualizer` instance configured to work with the window as the scrollElement.
​
+## `useVirtualizerSnapshot`
+​
+```tsx
+function useVirtualizerSnapshot<
+ TScrollElement extends Element,
+ TItemElement extends Element,
+>(
+ options: PartialKeys<
+ ReactVirtualizerSnapshotOptions<TScrollElement, TItemElement>,
+ 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'
+ >,
+): VirtualizerSnapshot<TScrollElement, TItemElement>
+```