Skip to content

Commit 4f54dab

Browse files
cleanup
1 parent 40587bb commit 4f54dab

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

packages/react/README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export const TodoListDisplaySuspense = () => {
272272
};
273273
```
274274

275-
## Preventing unecessary renders
275+
## Preventing Unnecessary Renders
276276

277277
The `useQuery` hook returns a stateful object which contains query fetching/loading state values and the query result set data.
278278

@@ -294,21 +294,23 @@ The returned object is a new JS object reference whenever the internal state cha
294294
function MyWidget() {
295295
// ... Widget code
296296
// result is an object which contains `isLoading`, `isFetching`, `data` members.
297-
const result = useQuery(...)
297+
const {data, error, isLoading} = useQuery(...)
298298

299299
// ... Widget code
300300

301301
return (
302-
// Other components
303-
// MyWatchedWidget will rerender whenever the watched query state changes
304-
// (MyWatchedWidget will also rerender if the result object is unchanged if it is not memoized)
305-
<MyWatchedWidget watchedResult={result}>
302+
// ... Other components
303+
304+
// If MyWatchedWidget is not memoized
305+
// - It will rerender on any state change of the watched query. E.g. if isFetching alternates
306+
// If MyWatchedWidget is memoized
307+
// - It will re-render if the data reference changes. By default the data reference changes after any
308+
// change to the query's dependant tables. This can be optimized by using Incremental queries.
309+
<MyWatchedWidget watchedResult={data}>
306310
)
307311
}
308312
```
309313

310-
The above example is incomplete, but is required for the optimizations below.
311-
312314
### Incremental Queries
313315

314316
By default watched queries are queried whenever a change to the underlying tables has been detected. These changes might not be relevant to the actual query, but will still trigger a query and `data` update.
@@ -372,7 +374,7 @@ function MyWidget() {
372374
// The `data` reference will only be changed if there have been changes since the previous value.
373375
// When reportFetching == false the object returned from useQuery will only be changed when the data, isLoading or error state changes.
374376
// This method performs a comparison in memory in order to determine changes.
375-
const { data, isLoading, isFetching } = useQuery(`SELECT * FROM cats WHERE breed = 'tabby'`, [], {
377+
const { data, isLoading } = useQuery(`SELECT * FROM cats WHERE breed = 'tabby'`, [], {
376378
processor: {
377379
mode: 'comparison',
378380
comparator: new ArrayComparator({
@@ -388,7 +390,6 @@ function MyWidget() {
388390
// Other components
389391
// The data array is the same reference if no changes have occurred between fetches
390392
// Note: The array is a new reference is there are any changes in the result set (individual row object references are not preserved)
391-
// Note: CatCollection requires memoization in order to prevent re-rendering (due to the parent re-rendering on fetch)
392393
<CatCollection cats={data}>
393394
)
394395
}

packages/react/tests/useQuery.test.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,4 @@ describe('useQuery', () => {
361361
expect(newResult.current.isLoading).toEqual(false);
362362
expect(newResult.current.data.length).toEqual(1);
363363
});
364-
365-
// TODO: Add tests for powersync.onChangeWithCallback path
366364
});

0 commit comments

Comments
 (0)