table icon indicating copy to clipboard operation
table copied to clipboard

Empty data causes infinite looping

Open tiagooliveira08 opened this issue 2 years ago • 11 comments

Describe the bug

Following the oficial example virtualized-infinite-scrolling and setting the data as empty causes infinite looping, from what I investigated this happens when calling the getCoreRowModel function.

Your minimal, reproducible example

https://codesandbox.io/s/inspiring-ardinghelli-6hrj4z?file=/src/main.tsx

Steps to reproduce

Go to console and view logs

Expected behavior

With the empty array in the data property it shouldn't cause infinite loop

How often does this bug happen?

every time

Screenshots or Videos

image

Platform

Alls

react-table version

8.7.0

TypeScript version

No response

Additional context

No response

Terms & Code of Conduct

  • [X] I agree to follow this project's Code of Conduct
  • [X] 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.

tiagooliveira08 avatar Nov 25 '22 13:11 tiagooliveira08

this is also happening on a dashboard I'm working on.

tiagooliveira08 avatar Nov 25 '22 13:11 tiagooliveira08

I'm also seeing this when selecting rows.

mttigg avatar Dec 08 '22 23:12 mttigg

happens to me too, it makes the library unusable

DoronTorangy avatar Dec 22 '22 18:12 DoronTorangy

Guys, you pass new instance of data with each render. data has to be stable. If you need empty array, at least declare it next to the component const emptyArray = []; and use that instead of inlined []. Because [] !== [], but emptyArray === emptyArray

mattleonowicz avatar Dec 23 '22 12:12 mattleonowicz

I tried to memoized where my data is coming from, so its doesn't change every new render:

const customerListMemo = useMemo(
    () => Object.entries(customerList || {}),
    [customerList]
  );

  const table = useReactTable({
    data: customerListMemo,
    columns,
    getCoreRowModel: getCoreRowModel(),
    debugTable: true,
  });

This solved my problem

tifandotme avatar Mar 28 '23 00:03 tifandotme

I just ran into this issue. It took a long time to figure out that it was useReactTable that was causing the infinite rerenders.

useReactTable.data doesn't accept undefined so obviously I did useReactTable({ data: myData ?? [] }) 😭

Shouldn't this be documented somewhere? And/Or shown how to deal with undefined data in the examples?

Or even better (maybe) useReactTable.data should accept undefined

snebjorn avatar Aug 03 '23 08:08 snebjorn

I just ran into this issue. It took a long time to figure out that it was useReactTable that was causing the infinite rerenders.

useReactTable.data doesn't accept undefined so obviously I did useReactTable({ data: myData ?? [] }) 😭

Shouldn't this be documented somewhere? And/Or shown how to deal with undefined data in the examples?

Or even better (maybe) useReactTable.data should accept undefined

@snebjorn, did you find the best way to do it?

MDziurakh avatar Aug 30 '23 10:08 MDziurakh

yes, as mentioned above

// outside component
const emptyArray = []

// inside component
useReactTable({ data: myData ?? emptyArray })

snebjorn avatar Aug 30 '23 12:08 snebjorn

any update on this? the above answer does solve the issue, but i dont think it should be use that way every time, it should be fixed from the package

galih9 avatar Oct 20 '23 07:10 galih9

any update on this? the above answer does solve the issue, but i dont think it should be use that way every time, it should be fixed from the package

You would have to change how React fundamentally works. And rerendering when the data changes is a good, and expected behavior. It seems to just not be obvious to all that that is what can happen here if data doesn't have a stable reference.

KevinVandy avatar Oct 21 '23 01:10 KevinVandy

 // Ensure data is an array and has a stable identity
  const tableData = useMemo(() => data ?? [], [data]);

  const table = useReactTable({
    columns,
    data: tableData,
  });

This helped me avoid infinite looping.

admondtamang avatar Feb 21 '24 11:02 admondtamang