This site counts visits with analytics cookies only if you accept. Read the consent

Skip to content

ExamplesBasic Requests

Basic Requests

Simple GET and POST requests using the proxy mode with React Query hooks.

GET — Fetch Users

useQuery fetches when the component mounts. What it fetched is cached and fetched again when it goes stale.

ts
import { q } from "@/lib/query"

// useQuery fetches on mount, and caches what it fetched
const { data, isLoading, error } = q.users.useQuery("getAll")
q.users.useQuery("getAll")SANDBOX
sending…
undefined

Requests

as the browser recorded them and the site's server told of them

// no request yet: run the call

useQuery docs

POST — Create User

useMutation sends on demand. Run it to create a user: once it is created, the list above is fetched again and holds it.

ts
import { keys, q } from "@/lib/query"

// invalidate: the list above is fetched again once the user is created
const { mutate, data, isPending } = q.users.useMutation("create", {
  invalidate: [keys("users", "getAll")],
})

mutate({ name: "Jane Doe", email: "jane@example.com", role: "user" })
q.users.useMutation("create")SANDBOX
idle
// run the request to see the response

Requests

as the browser recorded them and the site's server told of them

// no request yet: run the call

useMutation docs

On Demand — Imperative Fetch

A query that waits: enabled: false holds it until refetch() runs it — here, when you run it.

ts
import { q } from "@/lib/query"

// enabled: false holds the query until refetch() runs it
const { data, isFetching, refetch } = q.users.useQuery("getById", {
  variables: { id: 1 },
  enabled: false,
})

refetch()
q.users.useQuery("getById", { enabled: false })SANDBOX
idle
// run the request to see the response

Requests

as the browser recorded them and the site's server told of them

// no request yet: run the call