Skip to content

Commit

Permalink
Add support for enabled to useInfiniteQuery (connectrpc#338)
Browse files Browse the repository at this point in the history
Porting support that was already added to `useQuery` in connectrpc#318.

Fixes connectrpc#336
  • Loading branch information
paul-sachs authored Feb 8, 2024
1 parent 631ca5b commit 74b0c27
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
28 changes: 28 additions & 0 deletions packages/connect-query/src/use-infinite-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,32 @@ describe("useSuspenseInfiniteQuery", () => {
],
});
});

it("can be disabled without explicit disableQuery", () => {
const { result } = renderHook(
() => {
return useInfiniteQuery(
methodDescriptor,
{
page: 0n,
},
{
getNextPageParam: (lastPage) => lastPage.page + 1n,
pageParamKey: "page",
enabled: false,
},
);
},
wrapper(
{
defaultOptions,
},
mockedPaginatedTransport,
),
);

expect(result.current.data).toBeUndefined();
expect(result.current.isPending).toBeTruthy();
expect(result.current.isFetching).toBeFalsy();
});
});
5 changes: 4 additions & 1 deletion packages/connect-query/src/use-infinite-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ export function useInfiniteQuery<
pageParamKey,
callOptions,
});
return tsUseInfiniteQuery({ ...options, ...baseOptions });
// The query cannot be enabled if the base options are disabled, regardless of
// incoming query options.
const enabled = baseOptions.enabled && (options.enabled ?? true);
return tsUseInfiniteQuery({ ...options, ...baseOptions, enabled });
}

/**
Expand Down

0 comments on commit 74b0c27

Please sign in to comment.