Skip to content

Commit 6b6e73f

Browse files
committed
Guard against loadNextPage re-use
1 parent 70b2bfb commit 6b6e73f

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Changelog
22
=========
33

4-
4.0.8 (2022-??-??)
4+
4.0.9 (2022-??-??)
55
------------------
66

77
* #94: Complete rewrite of `useInfiniteCollection` it had still more issues

src/hooks/use-infinite-collection.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function useInfiniteCollection<T = any>(resourceLike: ResourceLike<any>,
7777

7878
const rel = options?.rel || 'item';
7979

80-
const items = useRef<Resource<T>[]>([]);
80+
const [items, setItems] = useState<Resource<T>[]>([]);
8181

8282
// Are there more pages?
8383
const nextPageResource = useRef<Resource|null>(null);
@@ -102,7 +102,7 @@ export function useInfiniteCollection<T = any>(resourceLike: ResourceLike<any>,
102102

103103
if (!bc.loading) {
104104
// The 'base collection' has stopped loading, so lets set the first page.
105-
items.current = bc.resourceState.followAll(rel);
105+
setItems(bc.resourceState.followAll(rel));
106106
nextPageResource.current = bc.resourceState.links.has('next') ? bc.resourceState.follow('next') : null;
107107
setLoading(false);
108108
}
@@ -136,10 +136,10 @@ export function useInfiniteCollection<T = any>(resourceLike: ResourceLike<any>,
136136
nextPageResource.current = nextPageState.links.has('next') ? nextPageState.follow('next') : null;
137137

138138
// Add new resources to page data
139-
items.current = [
140-
...items.current,
139+
setItems([
140+
...items,
141141
...nextPageState.followAll(rel)
142-
];
142+
]);
143143

144144
} catch (err:any) {
145145
error.current = err;
@@ -151,7 +151,7 @@ export function useInfiniteCollection<T = any>(resourceLike: ResourceLike<any>,
151151
return {
152152
loading: bc.loading || loading,
153153
error: bc.error ?? error.current ?? null,
154-
items: items.current,
154+
items,
155155
hasNextPage: nextPageResource.current !== null,
156156
loadNextPage,
157157
};

test/hooks/use-infinite-collection.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useEffect } from 'react';
33
import { render, screen, storeInCache, waitFor } from '../test-utils';
44
import { useInfiniteCollection, Resource, useResource } from '../../src';
55

6-
describe('useResource', () => {
6+
describe('useInfiniteCollection', () => {
77

88
storeInCache('/page/1', {
99
_links: {

0 commit comments

Comments
 (0)