|
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. |
6 | 10 |
|
@@ -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. |
69 | 73 |
|
70 | 74 | ```JSX |
71 | 75 | // TodoListDisplay.jsx |
@@ -119,8 +123,7 @@ export const TodoListsDisplayDemo = () => { |
119 | 123 |
|
120 | 124 | ### Suspense |
121 | 125 |
|
122 | | -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. |
| 126 | +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. |
124 | 127 |
|
125 | 128 | ```JSX |
126 | 129 | // TodoListDisplaySuspense.jsx |
@@ -154,7 +157,7 @@ export const TodoListDisplaySuspense = () => { |
154 | 157 |
|
155 | 158 | #### Blocking navigation on Suspense |
156 | 159 |
|
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). |
| 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). |
158 | 161 |
|
159 | 162 | > Note: In this example, the `<Suspense>` boundary is intentionally omitted to delegate the handling of the suspending state to the router. |
160 | 163 |
|
|
0 commit comments