Skip to content

Commit

Permalink
fix: remove unnecessary useMemo
Browse files Browse the repository at this point in the history
  • Loading branch information
psteinroe committed Aug 8, 2023
1 parent b4495da commit 92caf9e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-balloons-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@supabase-cache-helpers/postgrest-swr": patch
---

fix: remove unnecessary useMemo calls
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,9 @@ function useInfiniteOffsetPaginationQuery<

const [currentPageIndex, setCurrentPageIndex] = useState(0);

const { data: parsedData, hasMore } = useMemo(() => {
return {
data: (data ?? []).map((p) => p.data),
hasMore:
Array.isArray(data) && data.length > 0 && data[data.length - 1].hasMore,
};
}, [data]);
const parsedData = (data ?? []).map((p) => p.data);
const hasMore =
Array.isArray(data) && data.length > 0 && data[data.length - 1].hasMore;

return {
pages: parsedData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function useOffsetInfiniteQuery<
Relationships
> | null,
config?: SWRInfiniteConfiguration & { pageSize?: number }
): UseInfiniteQueryReturn<Result> {
): UseOffsetInfiniteQueryReturn<Result> {
return useSWRInfinite<
Exclude<PostgrestResponse<Result>['data'], null>,
PostgrestError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function useOffsetInfiniteScrollQuery<
Relationships
> | null,
config?: SWRInfiniteConfiguration & { pageSize?: number }
): UseInfiniteScrollQueryReturn<Result> {
): UseOffsetInfiniteScrollQueryReturn<Result> {
const { data, setSize, size, isValidating, ...rest } = useSWRInfinite<
PostgrestHasMorePaginationResponse<Result>,
PostgrestError
Expand Down Expand Up @@ -106,16 +106,11 @@ function useOffsetInfiniteScrollQuery<
}
);

const { data: flatData, hasMore } = useMemo(() => {
return {
data: (data ?? []).flatMap((p) => p.data),
hasMore:
Array.isArray(data) && data.length > 0 && data[data.length - 1].hasMore,
};
}, [data]);
const hasMore =
Array.isArray(data) && data.length > 0 && data[data.length - 1].hasMore;

return {
data: flatData,
data: (data ?? []).flatMap((p) => p.data),
size,
setSize,
loadMore: hasMore ? () => setSize(size + 1) : null,
Expand Down

0 comments on commit 92caf9e

Please sign in to comment.