Skip to content

Commit

Permalink
fix: Tanstack react query watch issues (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chriztiaan authored Oct 28, 2024
1 parent 30825f3 commit 6408783
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-pens-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/tanstack-react-query': patch
---

Fixed issue with queries not reacting to local data changes or causing an infinite loop on certain sql parameters.
9 changes: 6 additions & 3 deletions packages/tanstack-react-query/src/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ function useQueryCore<
}
}

const stringifiedParams = JSON.stringify(queryParameters);
const stringifiedKey = JSON.stringify(options.queryKey);

const fetchTables = async () => {
try {
const tables = await powerSync.resolveTables(sqlStatement, queryParameters);
Expand All @@ -104,7 +107,7 @@ function useQueryCore<
(async () => {
await fetchTables();
})();
}, [powerSync, sqlStatement, queryParameters]);
}, [powerSync, sqlStatement, stringifiedParams]);

const queryFn = React.useCallback(async () => {
if (error) {
Expand All @@ -116,7 +119,7 @@ function useQueryCore<
} catch (e) {
return Promise.reject(e);
}
}, [powerSync, query, parameters, options.queryKey, error]);
}, [powerSync, query, parameters, stringifiedKey, error]);

React.useEffect(() => {
if (error || !query) return () => {};
Expand All @@ -139,7 +142,7 @@ function useQueryCore<
}
);
return () => abort.abort();
}, [powerSync, options.queryKey, queryClient, tables, error]);
}, [powerSync, queryClient, stringifiedKey, tables, error]);

return useQueryFn(
{
Expand Down

0 comments on commit 6408783

Please sign in to comment.