queryFn is specified. This is useful for standardizing data fetching across your application.
Setting a Default Query Function
Configure a default query function when creating theQueryClient:
With a default query function, you can omit the
queryFn option in individual queries.Using Queries Without queryFn
Once configured, use queries without specifyingqueryFn:
Overriding the Default
You can still provide a customqueryFn for specific queries:
Advanced Default Function
Create a more sophisticated default function with error handling and authentication:Using with TypeScript
Type your default query function for better type safety:Query-Specific Defaults
Set defaults for specific query keys usingsetQueryDefaults:
1
Define query defaults
Use
setQueryDefaults with a query key prefix2
Defaults apply to matching keys
Any query with a matching key prefix inherits these defaults
3
Use queries without options
Queries automatically use the configured defaults
Partial Matching
Query defaults use partial key matching:Getting Query Defaults
Retrieve defaults for a specific query key:Merging with Other Defaults
Query defaults merge with global defaults and query-specific options:Real-World Example
A complete setup for a REST API:When to Use Default Query Functions
See Also
- Query Functions - Detailed query function guide
- Query Keys - Structuring effective query keys
- TypeScript - Type-safe query configuration