Skip to content

Commit 5abb174

Browse files
committed
fix: PowerSync react hooks, infinite re-renders when no parameters are provided
1 parent 9467a1c commit 5abb174

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

.changeset/strong-mice-smile.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@journeyapps/powersync-react": patch
3+
---
4+
5+
Fix issue with PowerSync React hooks that caused an infinite re-renders, when no `parameters` are provided

packages/powersync-react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"homepage": "https://github.com/journeyapps/powersync-react-native-sdk/packages/powersync-react/#readme",
2626
"dependencies": {
27-
"@journeyapps/powersync-sdk-common": "^0.0.1-alpha.0"
27+
"@journeyapps/powersync-sdk-common": "^0.0.1-alpha.1"
2828
},
2929
"peerDependencies": {
3030
"react": "*"
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
import React from 'react';
2-
import { usePowerSync } from './PowerSyncContext';
1+
import React from "react";
2+
import { usePowerSync } from "./PowerSyncContext";
33

44
/**
55
* A hook to access a single static query.
66
* For an updated result, use usePowerSyncWatchedQuery instead
77
*/
8-
export const usePowerSyncQuery = <T = any>(sqlStatement: string, parameters: any[] = []): T[] => {
8+
export const usePowerSyncQuery = <T = any>(
9+
sqlStatement: string,
10+
parameters: any[] = []
11+
): T[] => {
912
const powerSync = usePowerSync();
1013
if (!powerSync) {
1114
return [];
1215
}
13-
16+
// This will return the previous parameters until the contents of parameters array change,
17+
// thereby providing a stable array reference and preventing excessive useEffect runs
18+
const memoizedParams = React.useMemo(() => parameters, [...parameters]);
1419
const [data, setData] = React.useState<T[]>([]);
1520

1621
React.useEffect(() => {
1722
powerSync.execute(sqlStatement, parameters).then((result) => {
1823
setData(result.rows?._array ?? []);
1924
});
20-
}, [powerSync, sqlStatement, parameters]);
25+
//
26+
}, [powerSync, sqlStatement, memoizedParams]);
2127

2228
return data;
2329
};
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
import { SQLWatchOptions } from '@journeyapps/powersync-sdk-common';
2-
import React from 'react';
3-
import { usePowerSync } from './PowerSyncContext';
1+
import { SQLWatchOptions } from "@journeyapps/powersync-sdk-common";
2+
import React from "react";
3+
import { usePowerSync } from "./PowerSyncContext";
44

55
/**
66
* A hook to access the results of a watched query.
77
*/
88
export const usePowerSyncWatchedQuery = <T = any>(
99
sqlStatement: string,
1010
parameters: any[] = [],
11-
options: Omit<SQLWatchOptions, 'signal'> = {}
11+
options: Omit<SQLWatchOptions, "signal"> = {}
1212
): T[] => {
1313
const powerSync = usePowerSync();
1414
if (!powerSync) {
1515
return [];
1616
}
1717

18+
const memoizedParams = React.useMemo(() => parameters, [...parameters]);
19+
const memoizedOptions = React.useMemo(
20+
() => options,
21+
[JSON.stringify(options)]
22+
);
1823
const [data, setData] = React.useState<T[]>([]);
1924
const abortController = React.useRef(new AbortController());
2025

@@ -25,7 +30,7 @@ export const usePowerSyncWatchedQuery = <T = any>(
2530
(async () => {
2631
for await (const result of powerSync.watch(sqlStatement, parameters, {
2732
...options,
28-
signal: abortController.current.signal
33+
signal: abortController.current.signal,
2934
})) {
3035
setData(result.rows?._array ?? []);
3136
}
@@ -34,7 +39,7 @@ export const usePowerSyncWatchedQuery = <T = any>(
3439
return () => {
3540
abortController.current?.abort();
3641
};
37-
}, [powerSync, sqlStatement, parameters]);
42+
}, [powerSync, sqlStatement, memoizedParams, memoizedOptions]);
3843

3944
return data;
4045
};

0 commit comments

Comments
 (0)