Documentation

getQueryData

stable

Get 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

QueryKeyRequired

Unique 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 | undefined

The 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 cache
const currentUser = getQueryData(['user', 'current']);
if (currentUser) {
console.log('Current user:', currentUser.name);
// Use cached data for form submission
submitForm({ 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 data
const user = getQueryData(['user', 'current']);
if (user && user.role === 'admin') {
// Admin can access all routes
navigate(path);
} else if (user && path !== '/admin') {
// Regular user can access non-admin routes
navigate(path);
} else {
// Redirect to login
navigate('/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