Fix effect firing default getData for data prop variant

The effect was calling the default getData = () => [] even when the
consumer used the data prop, immediately clearing the loading state.
Now detects whether getData was explicitly provided and only runs the
async loading logic for that case. Also clears isLoading when data
transitions from undefined to defined.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Jonathan Roebuck <jroebuck@spotify.com>
This commit is contained in:
Jonathan Roebuck
2026-03-17 08:16:50 +00:00
parent 9314ff5162
commit d06dd51393
@@ -37,6 +37,7 @@ export function useCompletePagination<T extends TableItem, TFilter>(
filterFn,
searchFn,
} = options;
const hasGetData = 'getData' in options;
const { initialOffset = 0 } = paginationOptions;
const defaultPageSize = getEffectivePageSize(paginationOptions);
@@ -54,6 +55,11 @@ export function useCompletePagination<T extends TableItem, TFilter>(
// Load data on mount and when loadCount changes (reload trigger)
useEffect(() => {
if (data) {
setIsLoading(false);
return;
}
if (!hasGetData) {
return;
}
@@ -64,9 +70,9 @@ export function useCompletePagination<T extends TableItem, TFilter>(
(async () => {
try {
const result = getData();
const data = result instanceof Promise ? await result : result;
const resolvedData = result instanceof Promise ? await result : result;
if (!cancelled) {
setItems(data);
setItems(resolvedData);
setIsLoading(false);
}
} catch (err) {
@@ -80,7 +86,7 @@ export function useCompletePagination<T extends TableItem, TFilter>(
return () => {
cancelled = true;
};
}, [data, getData, loadCount]);
}, [data, getData, hasGetData, loadCount]);
// Reset offset when query changes (query object is memoized)
const prevQueryRef = useRef(query);