Skip to content

useQueryRef

useQueryRef<TQueryable>(queryable, options?): object

Defined in: packages/@livestore/react/src/useQuery.ts:60

Like useQuery, but also returns a reference to the underlying LiveQuery instance.

Usage

  • Accepts any Queryable<TResult>: a LiveQueryDef, SignalDef, a LiveQuery instance or a SQL QueryBuilder. Unions of queryables are supported and the result type is inferred via Queryable.Result<TQueryable>.
  • Creates an OpenTelemetry span per unique query, reusing it while the ref-counted resource is alive. The span name is updated once the dynamic label is known.
  • Manages a reference-counted resource under-the-hood so query instances are shared across re-renders and properly disposed once no longer referenced.

Parameters

  • queryable: The query definition/instance/builder to run and subscribe to.
  • options.store: Optional store to use; by default the store from LiveStoreContext is used.
  • options.otelContext: Optional parent otel context for the query span.
  • options.otelSpanName: Optional explicit span name; otherwise derived from the query label.

Returns

  • valueRef: A React ref whose current holds the latest query result. The type is Queryable.Result<TQueryable> with full inference for unions.
  • queryRcRef: The underlying reference-counted LiveQuery instance used by the store.

TQueryable extends Queryable<any>

TQueryable

Context

Parent otel context for the query

string

The name of the span to use for the query

Store<Any, { }>

object

queryRcRef: RcRef<LiveQuery<Result<TQueryable>>>

valueRef: RefObject<Result<TQueryable>>