Virtualizer does not work on React 18 StrictMode
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.
I can second that.
Workaround:
const rowVirtualizer = useVirtualizer({
// ...
})
useEffect(() => {
return () => {
rowVirtualizer.scrollElement = null
}
}, [rowVirtualizer])
Just shipped a fix for this. Thanks!