Skip to main content
Version: v10.0.0

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
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>)
Parameters
NameTypeDescription
query
Query & ServableQuery<Query>

Returns​

type UseLiveQueryReturnType<Query> = QueryState<ExtractQueryData<Query>> & { fetchNextPage: () => Promise<void>; refetch: () => Promise<void> };
useLiveQuery
NameTypeDescription
data
Data | undefined

undefined until the query has loaded, then the selected fields of every row

error
unknown

The last failure, also when a refresh failed and the rows stay

hasNextPage
boolean

Whether a paginated query has another page

isFetching
boolean

Whether a request for the query is in flight

stale
boolean

Whether the rows may be out of date: their subset is stale or its syncs are disconnected

status
QueryStatus

error only when a first load failed and there is nothing to show

fetchNextPage
() => Promise<void>

Loads the next page of a paginated query

refetch
() => Promise<void>

Loads the query's subset again