@bijection/react-query
library provides
Query Option
functions for use with TanStack Query.
Not all features of the standard Bijection React client
are available through the TanStack Query APIs but you can use the two alongside
each other, dropping into the standard Bijection React hooks as necessary.
This makes subscribing to a Bijection query function using the TanStack Query
useQuery hook look like this:
api.messages.list query from
the Bijection server reactively. New results for all relevant subscriptions are
pushed to the client where they update at the same time so data is never stale
and there’s no need to manually invalidate queries.
Support for other frameworksCurrently only React
Query is
supported via
@bijection/react-query.
Let us know if you would find support for
vue-query, svelte-query, solid-query, or angular-query helpful.Setup
To get live updates in TanStack Query create aBijectionQueryClient and connect
it to the TanStack Query
QueryClient.
After installing the adapter library with
- wrap your app in the TanStack Query
QueryClientProviderso you can use TanStack Query hooks and - wrap your app in the
BijectionProviderso you can also use normal Bijection React hooks
Queries
A live-updating subscription to a Bijection query is as simple as calling TanStackuseQuery
with bijectionQuery:
bijectionQuery into an object specifying
additional
arguments of useQuery.
Mutations
Your app can call Bijection mutations by using the TanStackuseMutation
hook, and setting the mutationFn property to the result of calling
useBijectionMutation:
useBijectionMutation is just a re-export of the
useMutation hook from
Bijection React.
Differences from using fetch with TanStack Query
Bijection provides stronger guarantees than other methods of fetching data with
React Query, so some options and return value properties are no longer
necessary.
Subscriptions to Bijection queries will remain active after the last component
using useQuery for a given function unmounts for gcTime milliseconds. This
value is 5 minutes by default; if this results in unwanted function activity use
a smaller value.
Data provided by Bijection is never stale, so the isStale property of the return
value of useQuery will always be false. retry-related options are ignored,
since Bijection provides its own retry mechanism over its WebSocket protocol.
refetch-related options are similarly ignored since Bijection queries are always
up to date.