API reference / @evolu/react-native / web / useQuery
Variable: useQuery()
const useQuery: <R>(query, options?) => QueryRows<R>;
Defined in: react/dist/useQuery.d.ts:31
Load and subscribe to the Query, and return an object with rows and row
properties that are automatically updated when data changes.
Note that useQuery uses React Suspense. It means every usage of useQuery blocks rendering until loading is completed. To avoid loading waterfall with more queries, use useQueries.
The promise option allows preloading queries before rendering, which can be
useful for complex queries that might take noticeable time even with local
data. However, this is rarely needed as local queries are typically fast.
Example
// Get all rows.
const rows = useQuery(allTodos);
// Get rows for a specific todo (the first row can be null).
const rows = useQuery(todoById(1));
// Get all rows, but without subscribing to changes.
const rows = useQuery(allTodos, { once: true });
// Preload a query (rarely needed).
const allTodosPromise = evolu.loadQuery(allTodos);
const rows = useQuery(allTodos, { promise: allTodosPromise });
Type Parameters
| Type Parameter |
|---|
R extends Row |
Parameters
| Parameter | Type |
|---|---|
query | Query<R> |
options? | Partial<{ once: boolean; promise: Promise<QueryRows<R>>; }> |
Returns
QueryRows<R>