feat(react-virtual): compatibility with the React compiler #851
main fromrefs/pull/851/head·opened This PR addresses a critical issue with useVirtualizer compatibility when used alongside the React compiler. Previously, the React compiler would cache results based on the virtualizer instance, preventing updates when virtual items changed.
The solution implements a wrapper using Object.create around the virtualizer instance.
This approach maintains the internal state while allowing the instance reference to change.
The internal state remains constant across updates, with only the instance reference being modified when virtual items change.
This change affects object property behavior. Spread operations and Object.keys() on the virtualizer now return empty results. This impacts code relying on these operations and could be considered a breaking change.
If this, or the getVirtualItems eager evaluation, is a cause of concern we could make the transformation opt-in via option flag or could even be implemented externally as a separate hook.
- 1
- packages/react-virtual/src/index.tsx line 53Outdated@@ -50,4 +50,4 @@5050 return instance._willUpdate()5151 })525253- return instance53+ return React.useMemo(() => Object.create(instance), [instance.getVirtualItems()])
We could move this function inside the virtual core and use the
memohelper to invalidate the instance generation.Should give us more flexibility and will be easier to cover more invalidations.
looking forward for this to get merged, especially since with react 19 rc1 things seem to start moving again
Thinking about this, if we want to move in this direction, useSyncExternalStore feels like a more natural fit compared to useMemo. We can make it as separate hook"
For now 'use no memo' seems like a reasonable temporary workaround! https://react.dev/learn/react-compiler#something-is-not-working-after-compilation
@piecyk
Would somthing like this work for you?
function useMemoSafeVirtualizer< TScrollElement extends Element | Window, TItemElement extends Element, >( options: VirtualizerOptions<TScrollElement, TItemElement>, ): Virtualizer<TScrollElement, TItemElement> { const [instance] = React.useState( () => new Virtualizer<TScrollElement, TItemElement>(options), ) instance.setOptions(options) React.useEffect(() => { return instance._didMount() }, []) useIsomorphicLayoutEffect(() => { return instance._willUpdate() }) return useSyncExternalStore( useCallback((onStoreChange) => instance.subscribe(onStoreChange), [instance]), () => instance.getMemoSafeInstance(), () => instance.getMemoSafeInstance(), ) }We would need to add subscribe and getMemoSafeInstance to the core:
getMemoSafeInstance = memo( () => [this.getVirtualItems()], () => Object.create(this), { key: process.env.NODE_ENV !== 'production' && 'getMemoSafeInstance', debug: () => this.options.debug, }, ) private notify = (sync: boolean) => { this.options.onChange?.(this, sync) this.subscribers.forEach(fn => fn(this, sync)); } subscribe = (callback) => { this.subscribers.add(callback) retrurn () => { this.subscribers.delete(callback) } }lachlancollins(GitHub profile, opens in a new tab) changed the title
⚠️ No Changeset found#
Latest commit: 51e38169fbea083f0b8a8c87857326992727ddd9
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
View your CI Pipeline Execution ↗ for commit 51e38169fbea083f0b8a8c87857326992727ddd9.
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 28s View ↗ nx run-many --target=build --exclude=examples/**✅ Succeeded 3s View ↗
☁️ Nx Cloud last updated this comment at
2025-03-08 01:14:44UTCOpen in Stackblitz
More templates
- @tanstack/virtual-example-angular-dynamic
- @tanstack/virtual-example-angular-fixed
- @tanstack/virtual-example-angular-infinite-scroll
- @tanstack/virtual-example-angular-padding
- @tanstack/virtual-example-angular-smooth-scroll
- @tanstack/virtual-example-angular-sticky
- @tanstack/virtual-example-angular-table
- @tanstack/virtual-example-angular-variable
- @tanstack/virtual-example-angular-window
- tanstack-lit-virtual-example-dynamic
- tanstack-lit-virtual-example-fixed
- tanstack-react-virtual-example-dynamic
- tanstack-react-virtual-example-fixed
- tanstack-react-virtual-example-infinite-scroll
- tanstack-react-virtual-example-padding
- tanstack-react-virtual-example-scroll-padding
- tanstack-react-virtual-example-smooth-scroll
- tanstack-react-virtual-example-sticky
- tanstack-react-virtual-example-variable
- tanstack-react-virtual-example-table
- tanstack-react-virtual-example-window
- tanstack-svelte-virtual-example-dynamic
- tanstack-svelte-virtual-example-fixed
- tanstack-svelte-virtual-example-infinite-scroll
- tanstack-svelte-virtual-example-smooth-scroll
- tanstack-svelte-virtual-example-sticky
- tanstack-svelte-virtual-example-table
- tanstack-vue-virtual-example-dynamic
- tanstack-vue-virtual-example-fixed
- tanstack-vue-virtual-example-infinite-scroll
- tanstack-vue-virtual-example-padding
- tanstack-vue-virtual-example-scroll-padding
- tanstack-vue-virtual-example-smooth-scroll
- tanstack-vue-virtual-example-sticky
- tanstack-vue-virtual-example-table
- tanstack-vue-virtual-example-variable
@tanstack/angular-virtual
npm i https://pkg.pr.new/@tanstack/angular-virtual@851@tanstack/lit-virtual
npm i https://pkg.pr.new/@tanstack/lit-virtual@851@tanstack/solid-virtual
npm i https://pkg.pr.new/@tanstack/solid-virtual@851@tanstack/react-virtual
npm i https://pkg.pr.new/@tanstack/react-virtual@851@tanstack/virtual-core
npm i https://pkg.pr.new/@tanstack/virtual-core@851@tanstack/svelte-virtual
npm i https://pkg.pr.new/@tanstack/svelte-virtual@851@tanstack/vue-virtual
npm i https://pkg.pr.new/@tanstack/vue-virtual@851commit:
51e3816Please merge this, it would be awesome!
Any timeline on this PR getting merged?
This is not really a fully featured fix, more like a POC.
Going to close to avoid unnecessary distractions.
gdorsi(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
gdorsi(GitHub profile, opens in a new tab) reviewed