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
@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.
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.
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.
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:
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:
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.