Skip to content

Commit f11f817

Browse files
committed
Fixed issue where useQuery() would not correctly trigger a new execution when the query or parameters changed while using StrictMode.
1 parent e256286 commit f11f817

File tree

3 files changed

+489
-498
lines changed

3 files changed

+489
-498
lines changed

.changeset/heavy-coats-serve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/react': patch
3+
---
4+
5+
Fixed issue where `useQuery()` would not correctly trigger a new execution when the query or parameters changed while using StrictMode.

packages/react/src/hooks/watched/useWatchedQuery.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export const useWatchedQuery = <RowType = unknown>(
1616
): QueryResult<RowType> | ReadonlyQueryResult<RowType> => {
1717
const { query, powerSync, queryChanged, options: hookOptions, active } = options;
1818

19+
const queryChangeRef = React.useRef(false);
20+
if (queryChanged && !queryChangeRef.current) {
21+
queryChangeRef.current = true;
22+
}
23+
1924
function createWatchedQuery() {
2025
if (!active) {
2126
return null;
@@ -44,14 +49,15 @@ export const useWatchedQuery = <RowType = unknown>(
4449
// Indicates that the query will be re-fetched due to a change in the query.
4550
// Used when `isFetching` hasn't been set to true yet due to React execution.
4651
React.useEffect(() => {
47-
if (queryChanged) {
52+
if (queryChangeRef.current) {
4853
watchedQuery?.updateSettings({
4954
query,
5055
throttleMs: hookOptions.throttleMs,
5156
reportFetching: hookOptions.reportFetching
5257
});
58+
queryChangeRef.current = false;
5359
}
54-
}, [queryChanged]);
60+
}, [queryChangeRef.current]);
5561

5662
return useNullableWatchedQuerySubscription(watchedQuery);
5763
};

0 commit comments

Comments
 (0)