React Query
Data fetching and caching with React hooks
React Query
The SDK integrates TanStack Query to provide data fetching, caching, and synchronization.
Setup
npm install @tanstack/react-queryPass the SDK's client.queryClient to the QueryClientProvider.
'use client'
import { QueryClientProvider } from '@tanstack/react-query'
import { client } from './client'
export function QueryProvider({ children }: { children: React.ReactNode }) {
return (
<QueryClientProvider client={client.queryClient}>
{children}
</QueryClientProvider>
)
}Read Hooks
| Hook | Description |
|---|---|
useQuery | List query |
useSuspenseQuery | Suspense list query |
useQueryById | Single item query by ID |
useSuspenseQueryById | Suspense single item query |
useInfiniteQuery | Infinite scroll |
useSuspenseInfiniteQuery | Suspense infinite scroll |
useQuery
const { data, isLoading, error } = client.query.useQuery(
{
collection: 'products',
options: {
limit: 10,
where: { status: { equals: 'published' } },
sort: '-createdAt',
}
},
{
// TanStack Query options (optional)
staleTime: 5 * 60 * 1000,
enabled: true,
}
)
// data: T[] (docs array)useQueryById
const { data: product, isLoading } = client.query.useQueryById({
collection: 'products',
id: 'product_id',
})
// data: T (document object)useInfiniteQuery
const {
data,
fetchNextPage,
hasNextPage,
isFetchingNextPage,
} = client.query.useInfiniteQuery({
collection: 'products',
pageSize: 20,
})
// data.pages: T[][] (array per page)Suspense Hooks
useSuspenseQuery, useSuspenseQueryById, and useSuspenseInfiniteQuery accept the same parameters, with data always available (the enabled option is not supported).
Mutation Hooks
| Hook | Description | Cache Invalidation |
|---|---|---|
useCreate | Create document | list cache |
useUpdate | Update document | all cache |
useRemove | Delete document | all cache |
useCreate
const { mutate: create, isPending } = client.query.useCreate({
collection: 'products',
})
create(
{ title: 'New Product', price: 10000 },
{
onSuccess: (data) => console.log('Created:', data.doc.id),
onError: (error) => console.error(error),
}
)useUpdate
const { mutate: update } = client.query.useUpdate({
collection: 'products',
})
update({ id: 'product_id', data: { title: 'Updated Product' } })useRemove
const { mutate: remove } = client.query.useRemove({
collection: 'products',
})
remove('product_id')Prefetching (SSR)
// Prefetch in server components
await client.query.prefetchQuery({ collection: 'products', options: { limit: 10 } })
await client.query.prefetchQueryById({ collection: 'products', id: 'product_id' })
await client.query.prefetchInfiniteQuery({ collection: 'products', pageSize: 20 })import { HydrationBoundary, dehydrate } from '@tanstack/react-query'
export default async function ProductsPage() {
await client.query.prefetchQuery({
collection: 'products',
options: { limit: 10 }
})
return (
<HydrationBoundary state={dehydrate(client.queryClient)}>
<ProductList />
</HydrationBoundary>
)
}Cache Management
// Invalidation
client.query.invalidateQueries('products') // All
client.query.invalidateQueries('products', 'list') // List only
client.query.invalidateQueries('products', 'detail') // Detail only
// Cache read/write
client.query.getQueryData('products', 'list', options)
client.query.setQueryData('products', 'detail', '123', updatedProduct)Customer Hooks
React Query hooks for customer authentication. Available on BrowserClient only.
useCustomerMe
Fetches the authenticated customer's profile. Automatically disabled when not authenticated.
const { data: profile, isLoading } = client.query.useCustomerMe()
// data: CustomerProfile | nulluseCustomerLogin
const { mutate: login, isPending } = client.query.useCustomerLogin({
onSuccess: (data) => console.log('Logged in:', data.customer.name),
})
login({ email: 'user@example.com', password: 'password' })
// Automatically invalidates useCustomerMe cacheuseCustomerRegister
const { mutate: register } = client.query.useCustomerRegister()
register({ name: 'John', email: 'john@example.com', password: 'secure123' })useCustomerLogout
const { mutate: logout } = client.query.useCustomerLogout()
logout()
// Removes all customer queries from cacheOther Customer Mutations
| Hook | Params | Cache |
|---|---|---|
useCustomerForgotPassword | email: string | — |
useCustomerResetPassword | { token, password } | — |
useCustomerVerifyEmail | token: string | Invalidates me |
useCustomerChangePassword | { currentPassword, newPassword } | — |
useCustomerRefreshToken | options? | Invalidates me |
Customer Cache Utilities
client.query.invalidateCustomerQueries() // Invalidate all customer queries
client.query.getCustomerData() // Read cached profile
client.query.setCustomerData(profile) // Write cached profile