petyosi / react-virtuoso

The most powerful virtual list component for React
https://virtuoso.dev
MIT License
5.25k stars 301 forks source link

[BUG] flushSync warning when isScrolling is turned on #804

Closed marcpropeller closed 1 year ago

marcpropeller commented 1 year ago

Describe the bug I have slightly modified the example at https://virtuoso.dev/prepend-items/. I receive the following console error when I reach the top of the list and then next items are prepended:

Warning: flushSync was called from inside a lifecycle method. React cannot flush when React is already rendering. Consider moving this call to a scheduler task or micro task

If I remove the isScrolling attribute the error goes away.

Reproduction https://codesandbox.io/s/cranky-babycat-o3zoln

To Reproduce Scroll to the top of the list, as the new items are prepended the error is produced.

Expected behavior Not to throw an error

Screenshots error

Desktop (please complete the following information):

Please let me know if you require any further information,

Thanks.

petyosi commented 1 year ago

I would advise you to find a workaround - basically, the sync setState call in isScrolling is causing the flushSync call. Something like this works I guess, not sure what's the purpose of the call.

      isScrolling={(scrolling) =>
        setTimeout(() => setIsScrolling(scrolling), 300)
      }

Unfortunately, the current flushSync is a hack that sits between React 17 and 18.

marcpropeller commented 1 year ago

@petyosi thanks for that information. I have a few other scenarios with this issue, so I'll see if delaying the setState fixes those too.