@powersync/react 0.0.0-dev-20240920103931 → 0.0.0-dev-20241008145307

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,19 +33,25 @@ export const useQuery = (query, parameters = [], options = { runQueryOnce: false
33
33
  const [isLoading, setIsLoading] = React.useState(true);
34
34
  const [isFetching, setIsFetching] = React.useState(true);
35
35
  const [tables, setTables] = React.useState([]);
36
- const memoizedParams = React.useMemo(() => queryParameters, [...queryParameters]);
36
+ const memoizedParams = React.useMemo(() => queryParameters, [JSON.stringify(queryParameters)]);
37
37
  const memoizedOptions = React.useMemo(() => options, [JSON.stringify(options)]);
38
38
  const abortController = React.useRef(new AbortController());
39
+ const previousQueryRef = React.useRef({ sqlStatement, memoizedParams });
40
+ // Indicates that the query will be re-fetched due to a change in the query.
41
+ // Used when `isFetching` hasn't been set to true yet due to React execution.
42
+ const shouldFetch = React.useMemo(() => previousQueryRef.current.sqlStatement !== sqlStatement ||
43
+ JSON.stringify(previousQueryRef.current.memoizedParams) != JSON.stringify(memoizedParams), [powerSync, sqlStatement, memoizedParams, isFetching]);
44
+ previousQueryRef.current = { sqlStatement, memoizedParams };
39
45
  const handleResult = (result) => {
46
+ setData(result);
40
47
  setIsLoading(false);
41
48
  setIsFetching(false);
42
- setData(result);
43
49
  setError(undefined);
44
50
  };
45
51
  const handleError = (e) => {
52
+ setData([]);
46
53
  setIsLoading(false);
47
54
  setIsFetching(false);
48
- setData([]);
49
55
  const wrappedError = new Error('PowerSync failed to fetch data: ' + e.message);
50
56
  wrappedError.cause = e;
51
57
  setError(wrappedError);
@@ -99,5 +105,5 @@ export const useQuery = (query, parameters = [], options = { runQueryOnce: false
99
105
  abortController.current?.abort();
100
106
  };
101
107
  }, [powerSync, sqlStatement, memoizedParams, memoizedOptions, tables]);
102
- return { isLoading, isFetching, data, error, refresh: fetchData };
108
+ return { isLoading, isFetching: isFetching || shouldFetch, data, error, refresh: fetchData };
103
109
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powersync/react",
3
- "version": "0.0.0-dev-20240920103931",
3
+ "version": "0.0.0-dev-20241008145307",
4
4
  "publishConfig": {
5
5
  "registry": "https://registry.npmjs.org/",
6
6
  "access": "public"
@@ -23,7 +23,7 @@
23
23
  "homepage": "https://docs.powersync.com",
24
24
  "peerDependencies": {
25
25
  "react": "*",
26
- "@powersync/common": "0.0.0-dev-20240920103931"
26
+ "@powersync/common": "^1.19.0"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@testing-library/react": "^15.0.2",