getQueryData
stableGet current data for a query
Synchronously get the current cached data for a query without triggering a fetch or subscription.
Core APISynchronous
Cache Access
getQueryData only returns data that's already cached. It won't trigger a fetch if the data doesn't exist.
Signature
function getQueryData<TData>(queryKey: QueryKey,options?: QueryOptions<TData>): TData | undefined
Parameters
queryKey
QueryKeyRequiredUnique identifier for the query. Can be a string or array of primitives.
options
QueryOptions<TData>Optional configuration object. If no fetcher is provided, uses a previously registered fetcher.
Returns
Returns
TData | undefinedThe current cached data for the query, or undefined if not cached
Examples
Basic Usage
Get cached data synchronously
import { getQueryData } from 'qortex-react';function handleFormSubmit() {// Get current user data from cacheconst currentUser = getQueryData(['user', 'current']);if (currentUser) {console.log('Current user:', currentUser.name);// Use cached data for form submissionsubmitForm({ userId: currentUser.id, ...formData });} else {console.log('No user data cached');}}
Conditional Logic
Use cached data for conditional operations
import { getQueryData } from 'qortex-react';function NavigationMenu() {const handleNavigation = (path: string) => {// Check if user is authenticated using cached dataconst user = getQueryData(['user', 'current']);if (user && user.role === 'admin') {// Admin can access all routesnavigate(path);} else if (user && path !== '/admin') {// Regular user can access non-admin routesnavigate(path);} else {// Redirect to loginnavigate('/login');}};return (<nav><button onClick={() => handleNavigation('/dashboard')}>Dashboard</button><button onClick={() => handleNavigation('/admin')}>Admin</button></nav>);}
Behavior
Synchronous
Returns data immediately without waiting for network requests
Cache Only
Only returns data that's already in the cache
No Side Effects
Does not trigger fetches or subscriptions
Type Safe
Returns properly typed data or undefined
Best Practices
Do's
- ✓Use for conditional logic based on cached data
- ✓Handle undefined return values appropriately
- ✓Use in event handlers and non-React code
- ✓Combine with other cache operations
Don'ts
- ✗Don't use in render functions (use useQuery instead)
- ✗Don't assume data will always be available
- ✗Don't use for data that must be fresh
- ✗Don't forget to handle undefined cases