Documentation

QueryState

stable

Query state type definition

Type definition for query state. Represents the current state of a query including data, loading status, error, and metadata.

TypeState

State Management

QueryState provides comprehensive state information for managing query lifecycle and user experience.

Signature

interface QueryState<TData, TError = Error> {
data: TData | undefined;
isLoading: boolean;
isFetching: boolean;
error: TError | null;
isError: boolean;
isSuccess: boolean;
isStale: boolean;
lastUpdated: number | undefined;
fetchCount: number;
}

Parameters

data

TData | undefinedRequired

The fetched data, undefined while loading or on error.

isLoading

booleanRequired

True if the query is currently fetching for the first time.

isFetching

booleanRequired

True if the query is currently fetching (including background refetches).

error

TError | nullRequired

Error object if the query failed, null otherwise.

isError

booleanRequired

True if the query failed with an error.

isSuccess

booleanRequired

True if the query succeeded and has data.

isStale

booleanRequired

True if the data is considered stale and should be refetched.

lastUpdated

number | undefinedRequired

Timestamp of when the data was last successfully fetched.

fetchCount

numberRequired

Number of times this query has been fetched.

Behavior

Reactive

State changes trigger component re-renders

Comprehensive

Provides all necessary state information

Type Safe

Full TypeScript support with proper type inference

Consistent

Consistent state representation across all queries

Best Practices

Do's

  • Use appropriate state properties for your use case
  • Handle all possible state combinations
  • Use state for conditional rendering and logic
  • Check state before performing actions

Don'ts

  • Don't assume data is always available
  • Don't forget to handle error states
  • Don't use state for side effects
  • Don't ignore loading states