react-spectrum: Table component randomly fails to render provided items.

Provide a general summary of the issue here

I’m using Table component in my Remix js application. Table items are provided by the loader function. Table sorting, search, pagination is handled with the URL query parameters. When user interacting with the table sorting, pagination or search for some time, the table component randomly stop showing content and renders empty state fallback component instead. I checked the items array, it contains data, but when I use React dev tools and check TableBody component, it shows that collection is empty, while props have items.

Screenshot 2024-05-16 at 23 36 06 Screenshot 2024-05-16 at 23 37 01

Here is my code

      <Table onSortChange={handleSorting} sortDescriptor={getSortDescriptor()}>
        <TableHeader columns={columns}>
          {(column) => (
            <Column isRowHeader={column.isRowHeader}>{column.name}</Column>
          )}
        </TableHeader>
        <TableBody items={tableRows} renderEmptyState={() => "no results"}>
          {(item) => (
            <Row aria-label="Table Row" columns={customerTableColumns}>
              {(column) => (
                <Cell aria-label="Table Cell">{item[column.id]}</Cell>
              )}
            </Row>
          )}
        </TableBody>
      </Table>

🤔 Expected Behavior?

Table should render items if provided.

😯 Current Behavior

Sometimes table does not render items.

💁 Possible Solution

Unfortunately I don’t have solution.

🔦 Context

Items are dynamic, provided by the Remix loader with the help of useLoaderData hook.

🖥️ Steps to Reproduce

Unable to reproduce it in sandbox. Might be SSR related.

Version

1.1.1

What browsers are you seeing the problem on?

Chrome

If other, please specify.

Remix js

What operating system are you using?

Mac OS

🧢 Your Company/Team

Arbonics

🕷 Tracking Issue

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 11
  • Comments: 19 (8 by maintainers)

Most upvoted comments

I’m having the same issue. Combination with react-aria table and react-query. Rows are correctly added to table initially, but the table removes all rows after click on one of the rows or sortable header… Stumped why this is happening since using a simple html table works fine.

@snowystinger @AndrewLeedham

FYI, I discovered that using an array index as the item ID, instead of a unique identifier like UUID4, prevents the issue. It seems that during sorting and searching, the IDs of the rows change, whereas the index remains constant. This can serve as a temporary workaround for now.

I can no longer reproduce with @joshuajaco’s reproduction on the latest nightly version. Going to close this as fixed by #7912.

Sharing my findings here.

Background for those not familiar with RAC architecture (like myself):

  • Perhaps due to some memoization, the bug is not visible unless some action is made to cause the component to re-render, hence the need of clicking a row to reproduce the issue. But underneath, the bug is cooking 🧑‍🍳
  • To render rows, you need a populated collection. If the collection is empty - rows are missing.
  • collection is updated by collection.addNode followed by collection.commit call.
  • In this case, it’s done by Document.updateCollection.
  • Document.updateCollection is called by Document.updateCollection and by useSSRCollectionNode which might be the key to this, because it makes the last successful call that updates the collection.

Findings:

  • The flow of building a (valid) collection is similar on the server and the client: Document.updateCollection is called several times, which updates the collection as described above.
  • BUT THEN, on the client, in Document, I can see a long series of this.mutatedNodes.add calls, followed by TableCollection.addNode call, as if the collection was being re-created from scratch for some reason. After this long series of calls though, Document.updateCollection never gets called by useSSRCollectionNode again (!), so the collection is never updated with all the added rows! So the next time the component renders, collection has a size of 0.
  • Quick, horrible, workaround I was able to find was to add this at the end of Document.addNode implementation:
        clearTimeout(this.__timeout);
        this.__timeout = setTimeout(() => {
            this.updateCollection();
        }, 0);
  • This workaround definitely does not fix the underlying issue. The underlying issue is that Document.updateCollection does not get called OR that the collection is being needlessly re-created after first render (and it shouldn’t need yet another Document.updateCollection in the first place). It might go deeper than that, I ran out of time.

Ready-to-use patch: @react-aria-collections-npm-3.0.0-alpha.3-04c359b98f.patch

We are having a similar issue in our next.js app.

I was able to deterministically reproduce it using a combination of the following:

The full reproduction is at https://github.com/joshuajaco/react-aria-table-nextjs-bug

We’re seeing a similar problem as well where the rows/table content disappears if a user rapidly tries to load the table (eg. switching between the column sorts or paginating the table) with dynamic content. The CollectionBranch component seems to contain the props/components needed to render the rows/items but does not render any of the items