virtual icon indicating copy to clipboard operation
virtual copied to clipboard

Virtualizer does not work on React 18 StrictMode

Open douweknook opened this issue 4 years ago • 3 comments

Describe the bug

Recently we updated our app to use the latest beta version of react-virtual. However we noticed some small issues and after some more investigation it seems like the virtualizer doesn't work on React 18 with StrictMode on.

What seems to be the cause is the useEffect hook running twice and thus invoking the cleanup function immediately after mounting here: https://github.com/TanStack/virtual/blob/beta/packages/react-virtual/src/index.tsx#L39-L41

As you can see in the attached example new items are not rendering on scroll.

Your minimal, reproducible example

https://codesandbox.io/s/competent-mclaren-r4zqg6?file=/src/index.tsx

Steps to reproduce

See the attached example CodeSandbox. If I remove the StrictMode component or if I revert the rendering back to using ReactDOM.render the virtual list works as expected again.

Expected behavior

I would expect the virtualizer to also work on the latest version of React.

How often does this bug happen?

Every time

Screenshots or Videos

https://user-images.githubusercontent.com/3264754/174014311-d1116d56-37a2-4f79-a523-a8369188131f.mov

Platform

I'm using Chrome on macOS but expect this to be cross browser/OS.

tanstack-virtual version

v3.0.0-beta.2

TypeScript version

v4.7.3

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.

douweknook avatar Jun 16 '22 07:06 douweknook

I can second that.

johnnypea avatar Jun 23 '22 11:06 johnnypea

Workaround:

  const rowVirtualizer = useVirtualizer({
    // ...
  })
  useEffect(() => {
    return () => {
      rowVirtualizer.scrollElement = null
    }
  }, [rowVirtualizer])

himself65 avatar Jul 14 '22 18:07 himself65

Just shipped a fix for this. Thanks!

tannerlinsley avatar Jul 19 '22 21:07 tannerlinsley