Skip to content

Fix TableView resize observer loop limit #5432

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

Merged
merged 4 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 21 additions & 1 deletion packages/@react-aria/virtualizer/src/ScrollView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ interface ScrollViewProps extends HTMLAttributes<HTMLElement> {
scrollDirection?: 'horizontal' | 'vertical' | 'both'
}

let isOldReact = React.version.startsWith('16.') || React.version.startsWith('17.');

function ScrollView(props: ScrollViewProps, ref: RefObject<HTMLDivElement>) {
let {
contentSize,
Expand Down Expand Up @@ -148,7 +150,25 @@ function ScrollView(props: ScrollViewProps, ref: RefObject<HTMLDivElement>) {
useLayoutEffect(() => {
updateSize();
}, [updateSize]);
useResizeObserver({ref, onResize: updateSize});
let raf = useRef<ReturnType<typeof requestAnimationFrame> | null>();
let onResize = () => {
if (isOldReact) {
raf.current ??= requestAnimationFrame(() => {
Copy link
Member Author

@snowystinger snowystinger Nov 17, 2023

Choose a reason for hiding this comment

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

React 16 and 17 don't batch like 18. We instead emulate that (to a degree) by waiting for a raf before submitting our state changes

We also tried unstable_batchupdates, which solved most of the issue. However, when the scrollbar appeared, we got the dreaded loop limit exceeded. This is likely because two updates were coming in faster than a single raf. First, the rect size change due to the resize of the window. Second, a rect size change due to the scroll bar appearing.

updateSize();
raf.current = null;
});
} else {
updateSize();
}
};
useResizeObserver({ref, onResize});
useEffect(() => {
return () => {
if (raf.current) {
cancelAnimationFrame(raf.current);
}
};
}, []);

let style: React.CSSProperties = {
// Reset padding so that relative positioning works correctly. Padding will be done in JS layout.
Expand Down
6 changes: 6 additions & 0 deletions packages/@react-spectrum/list/test/ListView.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,9 @@ describe('ListView', function () {
act(() => {
fireEvent(window, new Event('resize'));
});
act(() => {
jest.runAllTimers();
});
await user.tab();
expect(grid.scrollTop).toBe(0);

Expand Down Expand Up @@ -1475,6 +1478,9 @@ describe('ListView', function () {
act(() => {
fireEvent(window, new Event('resize'));
});
act(() => {
jest.runAllTimers();
});
expect(grid.scrollTop).toBe(0);

focusRow(tree, 'Item 1');
Expand Down