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 whenshapeis provided (comma-separated fields) - Adds
pageandsizeif provided - Uses React Query to fetch with a stable key that includes:
finalUrl,method,data,apiClient.defaults.baseURL, andapiClient.defaults.headers
- Disables retry for HTTP 401 errors; otherwise retries up to 3 times
- If
mockDatais provided, short-circuits and returns it immediately withisLoading: false
useSuspenseFetch with includeHeaders: false.
Examples
GET with shaping and pagination
POST with payload
Editor/mock data
Return values
resource– The typed response orundefinedwhile loadingisLoading– React Query loading flaghasError– React Query error flag (boolean)query– The rest of the React Query result (methods, status) minus the basic fields
Edge cases
urlisnull/falsy → hook is disabled (enabled: false) and no request is made- 401 Unauthorized → no retries (fast-fail to let auth state respond)
See also
- useTypedData – Strongly-typed wrapper around
useData - createShape – Type-safe shape builder for
_shapequery - LocationContext – Ensure you call with the correct
httpClient