For AI agents: the complete documentation index is available at https://navios.tech/llms.txt, the full documentation bundle is available at https://navios.tech/llms-full.txt, and this page is available as Markdown at https://navios.tech/docs/builder/react-query/index.md.

React Query quick start

@navios/react-query turns an endpoint declaration into TanStack Query hooks. Arguments, data and errors keep the types the declaration gives them, and cache keys are built for you. This page continues from the Builder quick start and uses the API, getUser and createUser declared there.

Install

npm install @navios/react-query@next @tanstack/react-query

@tanstack/react-query must be ^5.102.0 or newer. Your app also needs TanStack's QueryClientProvider around the components that use the hooks; see TanStack's installation guide if it is not set up yet.

Create a client

declareClient takes the builder and returns three factories: query, infiniteQuery and mutation. Create it once per builder.

queries.ts
import { declareClient } from '@navios/react-query'

import { API } from './api'
import { createUser, getUser } from './users'

export const client = declareClient({ api: API })

export const userQuery = client.query(getUser)
export const useCreateUser = client.mutation(createUser)

A query is an object with hooks and cache helpers attached. A mutation is itself a hook, which is why it is named like one.

The only other option is defaults, which adds segments to every cache key: see Keys and invalidation.

Read with a query

use is useQuery for the endpoint. It takes the same arguments object as the endpoint function.

import { userQuery } from './queries'

function UserCard({ userId }: { userId: string }) {
  const { data: user, isPending, error } = userQuery.use({ urlParams: { userId } })

  if (isPending) return <p>Loading…</p>
  if (error) return <p>{error.message}</p>

  return <h2>{user.name}</h2>
}

user has the output type of userSchema. error is the endpoint's EndpointError, the same value getUser.safe() would give you; Errors shows how to branch on it in a component.

Write with a mutation

Calling the mutation hook returns TanStack's mutation result. mutate takes the endpoint's arguments.

import { useCreateUser } from './queries'

function AddUserButton() {
  const { mutate, isPending } = useCreateUser()

  return (
    <button
      disabled={isPending}
      onClick={() => mutate({ data: { name: 'Ada', email: '[email protected]' } })}
    >
      Add Ada
    </button>
  )
}

To refresh queries after a mutation succeeds, list them in its invalidates setting. Mutations covers it.

Inline or existing endpoint

Each factory accepts an endpoint you already declared, as above, or the declaration itself:

// userSchema from the Builder quick start
export const userQuery = client.query({
  method: 'GET',
  url: '/users/$userId',
  responseSchema: userSchema,
})

userQuery.endpoint // the endpoint function, created for you with API.declareEndpoint

The inline form takes every option declareEndpoint takes. Settings that belong to the hook sit beside them in the same object; with an existing endpoint they go in a second argument:

client.query({ method: 'GET', url: '/users/$userId', responseSchema: userSchema, withResponse: true })
client.query(getUser, { withResponse: true })

Pass an existing endpoint when the declaration is shared with a server or called outside React. Declare inline when hooks are its only consumer. The two produce the same query and the same cache key.

Related: Queries, Mutations, API reference.