From 00fd6fbf73c0adf4cf0f1270c16962319af93722 Mon Sep 17 00:00:00 2001 From: Martin Pengelly-Phillips Date: Fri, 9 Aug 2024 22:10:39 +0100 Subject: [PATCH] Improve Solid adapter. Use `createEffect` to ensure scrollElement ref is connected to DOM and ownerDocument/window before attempting to update measurements for it. Otherwise, `virtualizer.targetWindow` will be set to null (in _willUpdate), the scrollElement rect to nothing and no items will be displayed. In addition, remove some logic that causes redundant work to be done (e.g. `setOptions` called multiple times, `_willUpdate` called on mount which is a no-op if the scollElement didn't change). Instead rely on the options reactivity to perform the bulk of the work. Also, reset virtual items store when options change to ensure that reactivity is maintained - e.g. so that `measureItem` is called again in a `` loop if `scrollMargin` changed. --- packages/solid-virtual/src/index.tsx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/solid-virtual/src/index.tsx b/packages/solid-virtual/src/index.tsx index 69ac34fd..15c2546b 100644 --- a/packages/solid-virtual/src/index.tsx +++ b/packages/solid-virtual/src/index.tsx @@ -9,7 +9,7 @@ import { } from '@tanstack/virtual-core' import { - createComputed, + createEffect, createSignal, mergeProps, onCleanup, @@ -55,22 +55,19 @@ function createVirtualizerBase< } const virtualizer = new Proxy(instance, handler) - virtualizer.setOptions(resolvedOptions) onMount(() => { const cleanup = virtualizer._didMount() - virtualizer._willUpdate() onCleanup(cleanup) }) - createComputed(() => { + createEffect(() => { virtualizer.setOptions( mergeProps(resolvedOptions, options, { onChange: ( instance: Virtualizer, sync: boolean, ) => { - instance._willUpdate() setVirtualItems( reconcile(instance.getVirtualItems(), { key: 'index', @@ -81,6 +78,8 @@ function createVirtualizerBase< }, }), ) + setVirtualItems([]) + virtualizer._willUpdate() virtualizer.measure() })