material-react-table icon indicating copy to clipboard operation
material-react-table copied to clipboard

TableBody component should respect isLoading state and not attempt to render empty state fallback during initial loading

Open YOOJS1205 opened this issue 10 months ago • 5 comments

material-react-table version

v3.1.0

react & react-dom versions

v18.2.0

Describe the bug and the steps to reproduce it

When isLoading is true, the table still attempts to access and render data properties, which can cause type errors. The table should not try to access or render any data during the initial loading state, and should only show the loading overlay.

Currently, the MRT_TableBody component only checks for empty rows (!rows.length) but doesn't consider the isLoading state. This causes two issues:

Type errors when accessing data properties during loading Unwanted empty state fallback being rendered behind the loading overlay

Steps to reproduce

  1. Create a table with async data loading:
const { data, isLoading } = useQuery(...)

const table = useMaterialReactTable({
  data: data ?? [],
  columns,
  state: {
    isLoading,
    showLoadingOverlay: true
    // when showSkeleton's value is false, Type Error occured
    showSkeleton: false,
  }
});
  1. During the initial loading state (isLoading: true):
  • Type errors occur as the table tries to access data properties
  • Empty state fallback is visible behind the loading overlay
  • The table attempts to process data operations even though we're loading

Expected behavior

  • When isLoading is true, the table should:
    1. Not attempt to access or process any data
    2. Not render the empty state fallback
    3. Only show the loading overlay
    4. Prevent any data-related operations until loading is complete

The MRT_TableBody component should check for isLoading state before attempting to render or process any data.

Minimal, Reproducible Example - (Optional, but Recommended)

const { data, isLoading } = useQuery(...)

const table = useMaterialReactTable({
  data: data ?? [],
  columns,
  state: {
    isLoading,
    showLoadingOverlay: true
    // when showSkeleton's value is false, Type Error occured
    showSkeleton: false,
  }
});
// MRT_TableBody.tsx

...
// does not check isLoading state
          (!rows.length ? (
            <tr
              style={{
                display: layoutMode?.startsWith('grid') ? 'grid' : undefined,
              }}
            >
              <td
                colSpan={table.getVisibleLeafColumns().length}
                style={{
                  display: layoutMode?.startsWith('grid') ? 'grid' : undefined,
                }}
              >

...

Screenshots or Videos (Optional)

Image

Do you intend to try to help solve this bug with your own PR?

Yes, I think I know how to fix it and will discuss it in the comments of this issue

Terms

  • [x] I understand that if my bug cannot be reliably reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

YOOJS1205 avatar Feb 06 '25 05:02 YOOJS1205

any workaround on that?

kamaladafrica avatar Apr 14 '25 16:04 kamaladafrica

https://github.com/KevinVandy/material-react-table/blob/8293b961523dc14878bf93af6122c6766e7c2233/packages/material-react-table/src/hooks/useMRT_TableInstance.ts#L222-L249

Here the logic shows isLoading || showSkeletons

statefulTableOptions.state.isLoading || 
       statefulTableOptions.state.showSkeletons

While it seems to me that it should be isLoading && showSkeletons

meesvandongen avatar Apr 16 '25 11:04 meesvandongen

@meesvandongen I agree Can I fix and open pull request?

YOOJS1205 avatar Apr 16 '25 11:04 YOOJS1205

I am no maintainer so I have no influence here whatsoever. Though I imagine @KevinVandy would be happy to merge it.

meesvandongen avatar Apr 16 '25 11:04 meesvandongen

any workaround on that?

@kamaladafrica I turn off showSekeletons flags and apply custom loading UI (add spinning row when state.isLoading is true)

YOOJS1205 avatar May 19 '25 15:05 YOOJS1205