Skip to content

Commit ead8e11

Browse files
committed
Changed how default query client is derived when not supplied as a function parameter.
1 parent 53fd64e commit ead8e11

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

.changeset/cold-grapes-occur.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/tanstack-react-query': minor
3+
---
4+
5+
Changed how default query client is derived when not supplied as a function parameter. Fixes some cases where deriving the query client happens too early.

packages/tanstack-react-query/src/hooks/useQuery.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function useQuery<TData = unknown, TError = Tanstack.DefaultError>(
2424

2525
export function useQuery<TData = unknown, TError = Tanstack.DefaultError>(
2626
options: UseBaseQueryOptions<Tanstack.UseQueryOptions<TData, TError>>,
27-
queryClient: Tanstack.QueryClient = Tanstack.useQueryClient()
27+
queryClient?: Tanstack.QueryClient
2828
) {
2929
return useQueryCore(options, queryClient, Tanstack.useQuery);
3030
}
@@ -44,7 +44,7 @@ export function useSuspenseQuery<TData = unknown, TError = Tanstack.DefaultError
4444

4545
export function useSuspenseQuery<TData = unknown, TError = Tanstack.DefaultError>(
4646
options: UseBaseQueryOptions<Tanstack.UseSuspenseQueryOptions<TData, TError>>,
47-
queryClient: Tanstack.QueryClient = Tanstack.useQueryClient()
47+
queryClient?: Tanstack.QueryClient
4848
) {
4949
return useQueryCore(options, queryClient, Tanstack.useSuspenseQuery);
5050
}
@@ -56,14 +56,14 @@ function useQueryCore<
5656
TQueryResult extends Tanstack.UseQueryResult<TData, TError> | Tanstack.UseSuspenseQueryResult<TData, TError>
5757
>(
5858
options: UseBaseQueryOptions<TQueryOptions>,
59-
queryClient: Tanstack.QueryClient,
59+
queryClient: Tanstack.QueryClient | undefined,
6060
useQueryFn: (options: TQueryOptions, queryClient?: Tanstack.QueryClient) => TQueryResult
6161
): TQueryResult {
6262
const powerSync = usePowerSync();
63-
6463
if (!powerSync) {
6564
throw new Error('PowerSync is not available');
6665
}
66+
const resolvedQueryClient = queryClient ?? Tanstack.useQueryClient();
6767

6868
let error: Error | undefined = undefined;
6969

@@ -106,7 +106,7 @@ function useQueryCore<
106106
const l = powerSync.registerListener({
107107
schemaChanged: async () => {
108108
await fetchTables();
109-
queryClient.invalidateQueries({ queryKey: options.queryKey });
109+
resolvedQueryClient.invalidateQueries({ queryKey: options.queryKey });
110110
}
111111
});
112112

@@ -132,7 +132,7 @@ function useQueryCore<
132132
powerSync.onChangeWithCallback(
133133
{
134134
onChange: () => {
135-
queryClient.invalidateQueries({
135+
resolvedQueryClient.invalidateQueries({
136136
queryKey: options.queryKey
137137
});
138138
},
@@ -146,13 +146,13 @@ function useQueryCore<
146146
}
147147
);
148148
return () => abort.abort();
149-
}, [powerSync, queryClient, stringifiedKey, tables]);
149+
}, [powerSync, resolvedQueryClient, stringifiedKey, tables]);
150150

151151
return useQueryFn(
152152
{
153153
...(resolvedOptions as TQueryOptions),
154154
queryFn: query ? queryFn : resolvedOptions.queryFn
155155
} as TQueryOptions,
156-
queryClient
156+
resolvedQueryClient
157157
);
158158
}

0 commit comments

Comments
 (0)