Solid: Virtualizer does not render any rows when using Suspense #1011
Describe the bug#
When using <Suspense> in solid-js with TanStack virtual, the virtualizer never renders any rows once the async data is resolved. The virtualizer receives the "count" of the data and appears to hold the data in the virtualizer, but the virtualizer never returns any rows.
I have tried a few other variations with using a signal for the ref and/or using queueMicrotask (from https://github.com/TanStack/virtual/pull/790), but neither resolves this.
I have reproduced this issue below using a variation of the Dynamic Solid example from https://github.com/TanStack/virtual/pull/658.
Your minimal, reproducible example#
https://codesandbox.io/p/devbox/admiring-smoke-s3klk5
Steps to reproduce#
- View the Synchronous List and see the virtualizer is working as expected.
- View the Async List and see the virtualizer never displays any rows.
Expected behavior#
The async list should render the rows once suspense is resolved.
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
- OS: Windows 10
- Browser: Google Chrome
- Version 136.0.7103.116 (Official Build) (64-bit)
tanstack-virtual version#
3.13.9
TypeScript version#
5.8.3
Additional context#
In my project, I am using TanStack Query to retrieve the data, TanStack Router for routing, and TanStack Table to display the data. In the example, I am not using any of these for simplicity, and the problem persists.
Terms & Code of Conduct#
- I agree to follow this project's Code of Conduct
- I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
Was this resolved? I am sitting with a similar issue. I am fetching the list async and it is clear that the items are actually inside the dropdown component, but when I click on the dropdown it is empty. When I close the dropdown it shows all the data for a split second before it closes. I am also able to see the data in the dropdown if I refresh the page and quickly click on the dropdown. I have spent hours today trying to resolve this, but I have had no luck.
@HaleyBoles @JSEvgeny @BuiltByPhillip Not yet resolved, unfortunately. If anyone has time to review this with the Solid adapter, that would be super helpful.
I had simular issue. I separated the scroller component from the other virtualizer logic. // VirtualList <div style={{ height:
${rowVirtualizer.getTotalSize()}px, }} className={cn("relative w-full", className)} > ..........//Scroller export function Scroller({ children, ref, }: { children: React.ReactNode; ref: React.RefObject<HTMLDivElement | null>; }) { return (
{children}); }In my keys this works
useEffect(() => { rowVirtualizer.measure(); }, []);
In getScrollElement function check if element is connected to the DOM before returning it
getScrollElement: () => {
if (!props.tableContainerRef?.isConnected) {
return null;
}
return props.tableContainerRef
}having the same issue
for me simply calling .measure() manually didn't help
this seems to be related to the fact that for suspenses solid creates the dom elements beforehand but doesn't mount them in dom (leading to an empty client rect), and the virtualizer core skips measuring the viewport if the scrollElement stays the same
i believe the proper fix would be to check for scrollElement.isConnected inside the library code and not measure anything if it's not.
i was able to work around this issue by using:
const virtualizer = createVirtualizer({
getScrollElement: () => parentRef?.isConnected ? parentRef : null,
// ...
})
as mentioned above. forcing a re-measure on the same element also works:
onMount(() => {
virtualizer.scrollElement = null
virtualizer.measure()
})
(alternatively, <Show when={data.state === 'ready'}> also works)
I didn't have much luck with the previous solutions, so I've included a ResizeObserver instance for now. Seems to work so far.
createEffect(() => {
let scrollElementHeight = 0;
const ro = new ResizeObserver(() => {
if (scrollElement.clientHeight && scrollElementHeight !== scrollElement.clientHeight) {
scrollElementHeight = scrollElement.clientHeight;
virtualizer.measure();
}
});
ro.observe(scrollElement);
onCleanup(() => ro.disconnect());
});clean99(GitHub profile, opens in a new tab) linked this to #1360
Sign in to join this conversation.
I had exactly the same issue with
getVirtualItemsreturning an empty array for data that was fetched asynchronously, which I thought was the reason. But apparently the issue was with a short delay for setting upparentRef. In my specific use-case it was even funnier, because I tried using virtualised inside a dropdown component with havingparentRefattached to a div that was not initially displayed (for closed dropdown 😅).My fix was to "re-initiate" virtualizer on dropdown open, by calling
measuremethod. I also found that there were_didMountand_willUpdatemethods that most likely would do the same trick, but decided not to use then as they are marked private.Not sure if it helps with your specific case, but just wanted to highlight that having
parentRefproperly attached to a DOM element seems to have big impact