Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/heavy-coats-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/react': patch
---

Fixed issue where `useQuery()` would not correctly trigger a new execution when the query or parameters changed while using StrictMode.
12 changes: 10 additions & 2 deletions packages/react/src/hooks/watched/useWatchedQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ export const useWatchedQuery = <RowType = unknown>(
): QueryResult<RowType> | ReadonlyQueryResult<RowType> => {
const { query, powerSync, queryChanged, options: hookOptions, active } = options;

// This ref is used to protect against cases where `queryChanged` changes multiple times too quickly to be
// picked up by the useEffect below. This typically happens when React.StrictMode is enabled.
const queryChangeRef = React.useRef(false);
if (queryChanged && !queryChangeRef.current) {
queryChangeRef.current = true;
}

function createWatchedQuery() {
if (!active) {
return null;
Expand Down Expand Up @@ -44,14 +51,15 @@ export const useWatchedQuery = <RowType = unknown>(
// Indicates that the query will be re-fetched due to a change in the query.
// Used when `isFetching` hasn't been set to true yet due to React execution.
React.useEffect(() => {
if (queryChanged) {
if (queryChangeRef.current) {
watchedQuery?.updateSettings({
query,
throttleMs: hookOptions.throttleMs,
reportFetching: hookOptions.reportFetching
});
queryChangeRef.current = false;
}
}, [queryChanged]);
}, [queryChangeRef.current]);

return useNullableWatchedQuerySubscription(watchedQuery);
};
Loading