@powersync/react 1.4.4 → 1.4.5

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.
@@ -36,19 +36,26 @@ export const useQuery = (query, parameters = [], options = { runQueryOnce: false
36
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]);
39
44
  const handleResult = (result) => {
45
+ setData(result);
40
46
  setIsLoading(false);
41
47
  setIsFetching(false);
42
- setData(result);
43
48
  setError(undefined);
49
+ previousQueryRef.current = { sqlStatement, memoizedParams };
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);
58
+ previousQueryRef.current = { sqlStatement, memoizedParams };
52
59
  };
53
60
  const fetchData = async () => {
54
61
  setIsFetching(true);
@@ -99,5 +106,5 @@ export const useQuery = (query, parameters = [], options = { runQueryOnce: false
99
106
  abortController.current?.abort();
100
107
  };
101
108
  }, [powerSync, sqlStatement, memoizedParams, memoizedOptions, tables]);
102
- return { isLoading, isFetching, data, error, refresh: fetchData };
109
+ return { isLoading, isFetching: isFetching || shouldFetch, data, error, refresh: fetchData };
103
110
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powersync/react",
3
- "version": "1.4.4",
3
+ "version": "1.4.5",
4
4
  "publishConfig": {
5
5
  "registry": "https://registry.npmjs.org/",
6
6
  "access": "public"