Skip to content

fix: do not dispatch loadMore if hook is still in loading state #8571

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 5 commits into from
Jul 18, 2025
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
18 changes: 17 additions & 1 deletion packages/@react-spectrum/s2/stories/TableView.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,23 @@
*/

import {action} from '@storybook/addon-actions';
import {ActionButton, Cell, Column, Content, Heading, IllustratedMessage, Link, MenuItem, MenuSection, Row, TableBody, TableHeader, TableView, TableViewProps, Text} from '../src';
import {
ActionButton,
Cell,
Column,
Content,
Heading,
IllustratedMessage,
Link,
MenuItem,
MenuSection,
Row,
TableBody,
TableHeader,
TableView,
TableViewProps,
Text
} from '../src';
import {categorizeArgTypes} from './utils';
import Filter from '../s2wf-icons/S2_Icon_Filter_20_N.svg';
import FolderOpen from '../spectrum-illustrations/linear/FolderOpen';
Expand Down
2 changes: 1 addition & 1 deletion packages/@react-stately/data/src/useAsyncList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ export function useAsyncList<T, C = string>(options: AsyncListOptions<T, C>): As
},
loadMore() {
// Ignore if already loading more or if performing server side filtering.
if (data.state === 'loadingMore' || data.state === 'filtering' || data.cursor == null) {
if (data.state === 'loading' || data.state === 'loadingMore' || data.state === 'filtering' || data.cursor == null) {
return;
}

Expand Down
50 changes: 50 additions & 0 deletions packages/@react-stately/data/test/useAsyncList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,56 @@ describe('useAsyncList', () => {
expect(result.current.items).toEqual(ITEMS);
});

it('should prevent loadMore from firing if in the middle of a load', async () => {
let load = jest.fn().mockImplementation(getItems);
let {result} = renderHook(
() => useAsyncList({load})
);

expect(load).toHaveBeenCalledTimes(1);
expect(result.current.isLoading).toBe(true);
expect(result.current.items).toEqual([]);

await act(async () => {
result.current.loadMore();
});

expect(result.current.isLoading).toBe(true);
expect(result.current.items).toEqual([]);
expect(load).toHaveBeenCalledTimes(1);

await act(async () => {
jest.runAllTimers();
});

expect(result.current.isLoading).toBe(false);
expect(result.current.items).toEqual(ITEMS);

await act(async () => {
result.current.reload();
});

expect(result.current.isLoading).toBe(true);
expect(result.current.items).toEqual([]);
expect(load).toHaveBeenCalledTimes(2);

await act(async () => {
result.current.loadMore();
});

expect(result.current.isLoading).toBe(true);
expect(result.current.items).toEqual([]);
expect(load).toHaveBeenCalledTimes(2);

await act(async () => {
jest.runAllTimers();
});

expect(result.current.isLoading).toBe(false);
expect(result.current.items).toEqual(ITEMS);
});


it('should ignore duplicate loads where first resolves first', async () => {
let load = jest.fn().mockImplementation(getItems2);
let sort = jest.fn().mockImplementation(() => new Promise(resolve => setTimeout(() => resolve({items: ITEMS2}), 100)));
Expand Down