Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react): compatibility with the React compiler #851

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react-virtual/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function useVirtualizerBase<
return instance._willUpdate()
})

return instance
return React.useMemo(() => Object.create(instance), [instance.getVirtualItems()])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could move this function inside the virtual core and use the memo helper to invalidate the instance generation.

Should give us more flexibility and will be easier to cover more invalidations.

}

export function useVirtualizer<
Expand Down
27 changes: 26 additions & 1 deletion packages/react-virtual/tests/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeEach, test, expect, vi } from 'vitest'
import * as React from 'react'
import { render, screen, fireEvent } from '@testing-library/react'
import { render, screen, fireEvent, renderHook } from '@testing-library/react'

import { useVirtualizer, Range } from '../src/index'

Expand Down Expand Up @@ -190,3 +190,28 @@ test('should handle handle height change', () => {
rerender(<List count={1} height={200} />)
expect(screen.queryByText('Row 0')).toBeInTheDocument()
})

test('should refresh the virtualizer instance when the items are changing (React Compiler)', () => {
const scrollElement = document.createElement('div')
const { rerender, result } = renderHook(
(props) =>
useVirtualizer({
count: props.count,
getScrollElement: () => scrollElement,
estimateSize: () => 50,
observeElementRect: (_, cb) => {
cb({ height: 100, width: 100 })
},
measureElement: () => 50,
}),
{
initialProps: { count: 0 },
},
)

const initialVirtualizer = result.current;

rerender({ count: 10 });

expect(result.current).not.toBe(initialVirtualizer)
})