Method UseQuery
- Namespace
- Velvet
- Assembly
- Velvet.Docs.dll
UseQuery<T>(QueryOptions<T>, QueryClient?)
UseQuery<TQueryFnData, TData>(QueryOptions<TQueryFnData, TData>, QueryClient?) for a query whose result holds the data its query function returns.
public static QueryResult<T> UseQuery<T>(QueryOptions<T> options, QueryClient? client = null)
Parameters
optionsQueryOptions<T>The key, the query function, and the options QueryOptions<TQueryFnData, TData> lists.
clientQueryClientThe client to read; null reads the one Ref provides, and throws when no Provider supplies one.
Returns
- QueryResult<T>
The entry's state as this render reads it.
Type Parameters
TThe query's data type. Every query sharing a key must use the same one.
UseQuery<TQueryFnData, TData>(QueryOptions<TQueryFnData, TData>, QueryClient?)
TanStack Query's useQuery. Reads the entry options' key names in a
QueryClient, fetches it with the options' query function when it has no data or its
data is stale, and re-renders the component when a property of the result it reads changes.
Components reading one key share one entry and one request in flight, and the entry keeps its
result after they unmount, so a component mounting over it later renders that result at once.
public static QueryResult<TData> UseQuery<TQueryFnData, TData>(QueryOptions<TQueryFnData, TData> options, QueryClient? client = null)
Parameters
optionsQueryOptions<TQueryFnData, TData>The key, the query function, and the options QueryOptions<TQueryFnData, TData> lists.
clientQueryClientThe client to read; null reads the one Ref provides, and throws when no Provider supplies one.
Returns
- QueryResult<TData>
The entry's state as this render reads it.
Type Parameters
TQueryFnDataThe entry's data type. Every query sharing a key must use the same one.
TDataThe result's data type, which Select makes.
Remarks
Not a Suspense hook: a query with no data yet returns Pending rather than
suspending, as useQuery does. Use<T>(Func<CancellationToken, VelvetTask<T>>, object?)
stays the cache-less use().
The component subscribes in a passive effect, as useQuery does, and a fetch that mounting
starts begins there. A change of key moves the subscription to the new key's entry: the result
shows that entry's data, or none, and nothing the old key's request delivers afterwards.
A failed request runs again up to Retry times before the
failure is the entry's error, and a request that lands is shared structurally with the data the entry
held (StructuralSharing), as useQuery does.