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
Copy file name to clipboardExpand all lines: packages/react/README.md
+161-6
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,10 @@
1
-
# React components for PowerSync
1
+
# React Hooks for PowerSync
2
2
3
-
## Context
3
+
The `powersync/react` package provides React hooks for use with the JavaScript Web SDK or React Native SDK. These hooks are designed to support reactivity, and can be used to automatically re-render React components when query results update or to access PowerSync connectivity status changes.
4
+
5
+
## Usage
6
+
7
+
### Context
4
8
5
9
Configure a PowerSync DB connection and add it to a context provider.
The provided PowerSync client status is available with the `useStatus` hook.
50
54
@@ -63,9 +67,9 @@ const Component = () => {
63
67
};
64
68
```
65
69
66
-
###Queries
70
+
##Reactive Queries
67
71
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.
72
+
The `useQuery` hook allows you to access the results of a watched query. 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 also allows you to access the results of a watched query, but its loading and fetching states are handled through [Suspense](https://react.dev/reference/react/Suspense). 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");
136
+
137
+
return (
138
+
<ul>
139
+
{todoLists.map((list) => (
140
+
<li key={list.id}>{list.name}</li>
141
+
))}
142
+
</ul>
143
+
);
144
+
};
145
+
146
+
147
+
exportconstTodoListDisplaySuspense= () => {
148
+
return (
149
+
<ErrorBoundary fallback={<div>Something went wrong</div>}>
150
+
<Suspense fallback={<div>Loading todo lists...</div>}>
151
+
<TodoListContent />
152
+
</Suspense>
153
+
</ErrorBoundary>
154
+
);
155
+
};
156
+
```
157
+
158
+
#### Blocking navigation on Suspense
159
+
160
+
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).
161
+
162
+
> 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");
176
+
177
+
return (
178
+
<ul>
179
+
{todoLists.map((list) => (
180
+
<li key={list.id}>{list.name}</li>
181
+
))}
182
+
</ul>
183
+
);
184
+
};
185
+
186
+
187
+
exportconstTodoListsPage= () => {
188
+
return (
189
+
<ErrorBoundary fallback={<div>Something went wrong</div>}>
190
+
<TodoListContent />
191
+
</ErrorBoundary>
192
+
);
193
+
};
194
+
```
195
+
196
+
#### Managing Suspense When Updating `useSuspenseQuery` Parameters
197
+
198
+
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