QueryState
stableQuery state type definition
Type definition for query state. Represents the current state of a query including data, loading status, error, and metadata.
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 | undefinedRequiredThe fetched data, undefined while loading or on error.
isLoading
booleanRequiredTrue if the query is currently fetching for the first time.
isFetching
booleanRequiredTrue if the query is currently fetching (including background refetches).
error
TError | nullRequiredError object if the query failed, null otherwise.
isError
booleanRequiredTrue if the query failed with an error.
isSuccess
booleanRequiredTrue if the query succeeded and has data.
isStale
booleanRequiredTrue if the data is considered stale and should be refetched.
lastUpdated
number | undefinedRequiredTimestamp of when the data was last successfully fetched.
fetchCount
numberRequiredNumber 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