Skip to content

Commit 1750ded

Browse files
fix parameters regression
1 parent cc21d51 commit 1750ded

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

.changeset/dull-days-sneeze.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+
Fix regression in useQuery where updating the supplied query parameters would not update the underlaying query.

packages/react/src/hooks/watched/watch-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const constructCompatibleQuery = <RowType>(
6868
execute: () => query.execute()
6969
};
7070
}
71-
}, [query, powerSync]);
71+
}, [query, powerSync, ...parameters]);
7272

7373
const queryChanged = checkQueryChanged(parsedQuery, options);
7474

packages/react/tests/useQuery.test.tsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,57 @@ describe('useQuery', () => {
292292
);
293293
});
294294

295+
it('should react to updated queries (simple update)', async () => {
296+
const db = openPowerSync();
297+
298+
const wrapper = ({ children }) => <PowerSyncContext.Provider value={db}>{children}</PowerSyncContext.Provider>;
299+
300+
let updateParameters = (params: string[]): void => {};
301+
const newParametersPromise = new Promise<string[]>((resolve) => {
302+
updateParameters = resolve;
303+
});
304+
305+
await db.execute(/* sql */ `
306+
INSERT INTO
307+
lists (id, name)
308+
VALUES
309+
(uuid (), 'first'),
310+
(uuid (), 'second')
311+
`);
312+
313+
const query = () => {
314+
const [parameters, setParameters] = React.useState<string[]>(['first']);
315+
316+
useEffect(() => {
317+
// allow updating the parameters externally
318+
newParametersPromise.then((params) => setParameters(params));
319+
}, []);
320+
321+
return useQuery('SELECT * FROM lists WHERE name = ?', parameters);
322+
};
323+
324+
const { result } = renderHook(query, { wrapper });
325+
326+
// We should only receive the first list due to the WHERE clause
327+
await vi.waitFor(
328+
() => {
329+
expect(result.current.data[0]?.name).toEqual('first');
330+
},
331+
{ timeout: 500, interval: 100 }
332+
);
333+
334+
// Now update the parameter
335+
updateParameters(['second']);
336+
337+
// We should now only receive the second list due to the WHERE clause and updated parameter
338+
await vi.waitFor(
339+
() => {
340+
expect(result.current.data[0]?.name).toEqual('second');
341+
},
342+
{ timeout: 500, interval: 100 }
343+
);
344+
});
345+
295346
it('should show an error if parsing the query results in an error', async () => {
296347
const db = openPowerSync();
297348

0 commit comments

Comments
 (0)