Skip to content
Source
Sign inSign up
TanStack/virtual

feat(solid-virtual): improve Solid adapter #790

Openmartinpengellyphillips(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/790/headopened

I encountered some issues using the Solid adapter with the latest Solid version (1.8). In particular, no items would display unless I added into my code a deferred setting of the element ref (e.g. ref={(el) => queueMicrotask(() => setRef(el))}).

Looking through the source I believe the issue stems from _willUpdate being called with a scrollElement that has not yet been connected to a targetWindow (due to the use of createComputed). This PR attempts to avoid this issue and the need for a workaround in consumer code as well as make a few other changes for optimisation and to maintain reactivity expectations.

--

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 <For> loop if scrollMargin changed.

    1. @@ -57,20 +57,17 @@
      5757 const virtualizer = new Proxy(instance, handler)
      58- virtualizer.setOptions(resolvedOptions)
      5958
      6059 onMount(() => {
      6160 const cleanup = virtualizer._didMount()
      62- virtualizer._willUpdate()
      6361 onCleanup(cleanup)
      6462 })
      6563
      66- createComputed(() => {
      64+ createEffect(() => {
      6765 virtualizer.setOptions(
      6866 mergeProps(resolvedOptions, options, {
      6967 onChange: (
      7068 instance: Virtualizer<TScrollElement, TItemElement>,
      7169 sync: boolean,
      7270 ) => {
      73- instance._willUpdate()
      7471 setVirtualItems(
      7572 reconcile(instance.getVirtualItems(), {
      7673 key: 'index',

      Should also make this 'key' rather than 'index' so that it uses any custom supplied key from getItemKey option for the diff.

      piecykcommented

      Yep, it should be key

    2. @@ -55,20 +55,17 @@
      5555 }
      5656
      5757 const virtualizer = new Proxy(instance, handler)
      58- virtualizer.setOptions(resolvedOptions)
      5958
      6059 onMount(() => {
      6160 const cleanup = virtualizer._didMount()
      62- virtualizer._willUpdate()
      6361 onCleanup(cleanup)
      6462 })
      6563
      66- createComputed(() => {
      64+ createEffect(() => {
      6765 virtualizer.setOptions(
      6866 mergeProps(resolvedOptions, options, {
      6967 onChange: (
      7068 instance: Virtualizer<TScrollElement, TItemElement>,
      7169 sync: boolean,
      7270 ) => {
      73- instance._willUpdate()
      7471 setVirtualItems(

      Should batch the set state calls together.

      piecykcommented

      What do you suggest here?

    3. @martinpengellyphillips big thanks for this, could you add also some test to check the basic stuff 🙏 😉 ?

    4. @martinpengellyphillips big thanks for this, could you add also some test to check the basic stuff 🙏 😉 ?

      @piecyk Sure! Should I follow general approach to tests from the react adapter?

    5. @martinpengellyphillips big thanks for this, could you add also some test to check the basic stuff 🙏 😉 ?

      @piecyk Sure! Should I follow general approach to tests from the react adapter?

      Could be, or you can try playwright, have it on todo for some time. Overall what you feel more comfortable with, we need to start with something.

    6. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      ☁️ Nx Cloud Report

      CI is running/has finished running commands for commit 00fd6fbf73c0adf4cf0f1270c16962319af93722. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

      📂 See all runs for this CI Pipeline Execution


      ✅ Successfully ran 2 targets

    7. Sent with 💌 from NxCloud.

  • commit: 00fd6fb

    @tanstack/lit-virtual

    pnpm add https://pkg.pr.new/@tanstack/lit-virtual@790

    @tanstack/react-virtual

    pnpm add https://pkg.pr.new/@tanstack/react-virtual@790

    @tanstack/solid-virtual

    pnpm add https://pkg.pr.new/@tanstack/solid-virtual@790

    @tanstack/svelte-virtual

    pnpm add https://pkg.pr.new/@tanstack/svelte-virtual@790

    @tanstack/virtual-core

    pnpm add https://pkg.pr.new/@tanstack/virtual-core@790

    @tanstack/vue-virtual

    pnpm add https://pkg.pr.new/@tanstack/vue-virtual@790

    Open in Stackblitz

    More templates

  • piecyk reviewed

    piecykreplied on packages/solid-virtual/src/index.tsx line 73

    Yep, it should be key

    piecykreplied on packages/solid-virtual/src/index.tsx line 71

    What do you suggest here?

    @@ -81,4 +78,6 @@
    8178 },
    8279 }),
    8380 )
    81+ setVirtualItems([])
    82+ virtualizer._willUpdate()
    8483 virtualizer.measure()
    piecykcommented

    Btw the measure here looks suspicious

  • lachlancollins changed the title

  • lachlancollins changed the title

  • Sign in to join this conversation.