You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The provided PowerSync client status is available with the `useStatus` hook.
50
50
@@ -63,7 +63,7 @@ const Component = () => {
63
63
};
64
64
```
65
65
66
-
###Queries
66
+
## Queries
67
67
68
68
Queries will automatically update when a dependant table is updated unless you set the `runQueryOnce` flag. You are also able to use a compilable query (e.g. [Kysely queries](https://github.com/powersync-ja/powersync-js/tree/main/packages/kysely-driver)) as a query argument in place of a SQL statement string.
The response from `useQuery` includes the `isLoading` and `isFetching` properties, which indicate the current state of data retrieval. This can be used to show loading spinners or conditional widgets.
The `useSuspenseQuery` hook is available to handle the loading/fetching state through suspense.
123
+
Unlike `useQuery`, the hook doesn't return `isLoading` or `isFetching` for the loading states nor `error` for the error state. These should be handled with variants of `<Suspense>` and `<ErrorBoundary>` respectively.
const { data:todoLists } =useSuspenseQuery("SELECT * FROM lists");
133
+
134
+
return (
135
+
<ul>
136
+
{todoLists.map((list) => (
137
+
<li key={list.id}>{list.name}</li>
138
+
))}
139
+
</ul>
140
+
);
141
+
};
142
+
143
+
144
+
exportconstTodoListDisplaySuspense= () => {
145
+
return (
146
+
<ErrorBoundary fallback={<div>Something went wrong</div>}>
147
+
<Suspense fallback={<div>Loading todo lists...</div>}>
148
+
<TodoListContent />
149
+
</Suspense>
150
+
</ErrorBoundary>
151
+
);
152
+
};
153
+
```
154
+
155
+
#### Blocking navigation on Suspense
156
+
157
+
When you provide a suspense fallback, suspending components will cause the fallback to render. Alternatively, React's [startTransition](https://react.dev/reference/react/startTransition) allows navigation to be blocked until the suspending components have completed, preventing the fallback from displaying. This behavior can be facilitated by your router — for example, react-router supports this with its [startTransition flag](https://reactrouter.com/en/main/upgrading/future#v7_starttransition).
158
+
159
+
> Note: In this example, the `<Suspense>` boundary is intentionally omitted to delegate the handling of the suspending state to the router.
const { data:todoLists } =useSuspenseQuery("SELECT * FROM lists");
173
+
174
+
return (
175
+
<ul>
176
+
{todoLists.map((list) => (
177
+
<li key={list.id}>{list.name}</li>
178
+
))}
179
+
</ul>
180
+
);
181
+
};
182
+
183
+
184
+
exportconstTodoListsPage= () => {
185
+
return (
186
+
<ErrorBoundary fallback={<div>Something went wrong</div>}>
187
+
<TodoListContent />
188
+
</ErrorBoundary>
189
+
);
190
+
};
191
+
```
192
+
193
+
#### Managing Suspense When Updating `useSuspenseQuery` Parameters
194
+
195
+
When data in dependent tables changes, `useSuspenseQuery` automatically updates without suspending. However, changing the query parameters causes the hook to restart and enter a suspending state again, which triggers the suspense fallback. To prevent this and keep displaying the stale data until the new data is loaded, wrap the parameter changes in React's [startTransition](https://react.dev/reference/react/startTransition) or use [useDeferredValue](https://react.dev/reference/react/useDeferredValue).
0 commit comments