useLiveQuery
Reads a
@hyper-fetch/db
query and keeps the component current as the collection changes. The query starts at a
collection, as in
db.post.where(...)
, and loads through the loader the db picks for it.
A query is data, so a query rebuilt on every render with the same structure is the same query: the hook takes no dependency array. It does not compile when no loader of the collection can serve the query.
import { useLiveQuery } from "@hyper-fetch/react"
Usage
const { data, status, error } = useLiveQuery(
db.postSummary.where((post) => eq(post.teamId, teamId)).orderBy((post) => post.createdAt, "desc"),
);
Parameters
useLiveQuery<Query>(query: Query & ServableQuery<Query>)
| Name | Type | Description |
|---|---|---|
| query | |
Returns
type UseLiveQueryReturnType<Query> = QueryState<ExtractQueryData<Query>> & { fetchNextPage: () => Promise<void>; refetch: () => Promise<void> };
| Name | Type | Description |
|---|---|---|
| data | |
|
| error | | The last failure, also when a refresh failed and the rows stay |
| hasNextPage | | Whether a paginated query has another page |
| isFetching | | Whether a request for the query is in flight |
| stale | | Whether the rows may be out of date: their subset is stale or its syncs are disconnected |
| status | |
|
| fetchNextPage | | Loads the next page of a paginated query |
| refetch | | Loads the query's subset again |
