Skip to main content
TanStack Query for Preact provides hooks for fetching, caching, and updating asynchronous data in your Preact applications. The API is nearly identical to React Query.

Installation

Setup

Wrap your application with QueryClientProvider:

Core Hooks

useQuery

Fetch and cache data with the useQuery hook:

useMutation

Perform side effects with mutations:

useInfiniteQuery

Implement infinite scrolling:

Suspense Support

useSuspenseQuery

Preact Query has full support for Suspense:
Use preact/compat to access Suspense features. The API is identical to React’s Suspense.

useSuspenseInfiniteQuery

Advanced Features

useQueries

Execute multiple queries in parallel:

Query Options Factory

Error Boundaries

Preact-Specific Considerations

Size Optimization

Preact Query is optimized for small bundle sizes, perfect for Preact’s lightweight philosophy:

Preact Signals Integration

While Preact Query uses hooks, you can integrate with Preact Signals:
Access signal values with .value when using them in query keys or functions.

Hydration

For SSR with Preact:

TypeScript

Full TypeScript support:

React vs Preact Differences

API Compatibility

The API is virtually identical to React Query:

Suspense with Compat

Performance

Automatic Request Cancellation

useMutationState

useIsFetching

Migration from React Query

Migrating from React Query is straightforward:
  1. Change package imports:
  1. Use preact/compat for Suspense:
  1. Everything else remains the same.
Preact Query maintains API parity with React Query, making migration seamless.

DevTools

Use Preact Query DevTools for debugging:
Setup:

Best Practices

Bundle Size

Preact is known for small bundles. Optimize further:

Preact Compat

Use compat mode for maximum React compatibility: