Skip to main content

useData

useData is a lightweight wrapper around React Query that standardizes HTTP calls, adds first-class support for server-side shaping, pagination, and an optional mockData override for editor scenarios. Source: src/api/fetchData.ts

Signature

Behavior

  • Adds _shape=... to the URL when shape is provided (comma-separated fields)
  • Adds page and size if provided
  • Uses React Query to fetch with a stable key that includes:
    • finalUrl, method, data, apiClient.defaults.baseURL, and apiClient.defaults.headers
  • Disables retry for HTTP 401 errors; otherwise retries up to 3 times
  • If mockData is provided, short-circuits and returns it immediately with isLoading: false
Note on caching: headers are part of the query key; token/header changes will revalidate. If you need header-agnostic caching, use useSuspenseFetch with includeHeaders: false.

Examples

GET with shaping and pagination

POST with payload

Editor/mock data

Return values

  • resource – The typed response or undefined while loading
  • isLoading – React Query loading flag
  • hasError – React Query error flag (boolean)
  • query – The rest of the React Query result (methods, status) minus the basic fields

Edge cases

  • url is null/falsy → hook is disabled (enabled: false) and no request is made
  • 401 Unauthorized → no retries (fast-fail to let auth state respond)

See also