Documentation

getQueryState

stable

Get complete query state

Synchronously get the complete state of a query including data, loading status, error, and metadata.

Core APIComplete State

Complete State Access

getQueryState provides access to the complete query state, including loading status, errors, and metadata.

Signature

function getQueryState<TData, TError = Error>(
queryKey: QueryKey,
options?: QueryOptions<TData>
): QueryState<TData, TError>

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

QueryState<TData, TError>

The complete query state. Always returns a state object, creating one if it doesn't exist.

Examples

Basic Usage

Get complete query state synchronously

import { getQueryState } from 'qortex-core';
function handleUserAction() {
const userState = getQueryState(['user', 'current']);
if (userState) {
console.log('User data:', userState.data);
console.log('Is loading:', userState.isLoading);
console.log('Is fetching:', userState.isFetching);
console.log('Error:', userState.error);
if (userState.isLoading) {
console.log('User data is still loading...');
} else if (userState.error) {
console.log('Failed to load user:', userState.error.message);
} else if (userState.data) {
console.log('User loaded successfully:', userState.data.name);
}
} else {
console.log('No user query found');
}
}

Conditional Operations

Use query state for conditional operations

import { getQueryState } from 'qortex-core';
function SmartButton() {
const handleClick = () => {
const productsState = getQueryState('products');
if (!productsState) {
// No products query exists, start fresh
fetchQuery('products', { fetcher: fetchProducts });
} else if (productsState.error) {
// Previous fetch failed, retry
invalidateQuery('products');
} else if (productsState.isFetching) {
// Already fetching, do nothing
console.log('Products are already being fetched');
} else {
// Data is available, proceed with action
console.log('Products ready:', productsState.data);
}
};
return (
<button onClick={handleClick}>
Load Products
</button>
);
}

Behavior

Complete State

Returns all query state including data, loading, error, and metadata

Synchronous

Returns state immediately without waiting for network requests

No Side Effects

Does not trigger fetches or subscriptions

Type Safe

Returns properly typed state or undefined

Best Practices

Do's

  • Use for conditional logic based on complete query state
  • Handle undefined return values appropriately
  • Use in event handlers and non-React code
  • Check multiple state properties for comprehensive logic

Don'ts

  • Don't use in render functions (use useQuery instead)
  • Don't assume state will always be available
  • Don't use for data that must be fresh
  • Don't forget to handle all possible state combinations