Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

overview.md

93 lines3.7 KB

1---
2id: overview
3title: Overview
4---
5​
6> IMPORTANT: The `@tanstack/lit-query` package is currently experimental and v0.1. Expect the Lit adapter API and docs to keep evolving. If you use it in production while it is early, pin the package to a patch version and upgrade deliberately.
7​
8The `@tanstack/lit-query` package is the Lit adapter for TanStack Query. It gives Lit applications reactive controller APIs for fetching, caching, synchronizing, and updating server state.
9​
10TanStack Query manages server state: data that is owned by a remote system, fetched asynchronously, shared across screens, and potentially changed by someone else at any time. It handles caching, request deduplication, stale data, background refetching, mutations, invalidation, pagination, and garbage collection.
11​
12Lit Query exposes those features through [Lit reactive controllers](https://lit.dev/docs/composition/controllers/). A Lit reactive controller is attached to a `ReactiveControllerHost`, usually a `LitElement`. Lit Query controllers subscribe to the `QueryClient`, request host updates when results change, and are cleaned up with the host lifecycle.
13​
14## Core APIs
15​
16Most Lit applications use these APIs:
17​
18- [`QueryClientProvider`](./reference/classes/QueryClientProvider.md) to provide a `QueryClient` through Lit context
19- [`createQueryController`](./reference/functions/createQueryController.md) for queries
20- [`createQueriesController`](./reference/functions/createQueriesController.md) for dynamic parallel queries
21- [`createMutationController`](./reference/functions/createMutationController.md) for mutations
22- [`createInfiniteQueryController`](./reference/functions/createInfiniteQueryController.md) for infinite queries
23- [`useIsFetching`](./reference/functions/useIsFetching.md), [`useIsMutating`](./reference/functions/useIsMutating.md), and [`useMutationState`](./reference/functions/useMutationState.md) for cache state indicators
24​
25The adapter also re-exports TanStack Query Core APIs from `@tanstack/lit-query`, so examples in the Lit docs use `@tanstack/lit-query` as the user-facing import path.
26​
27## A First Query
28​
29```ts
30import {
31 QueryClient,
32 QueryClientProvider,
33 createQueryController,
34} from '@tanstack/lit-query'
35import { LitElement, html } from 'lit'
36​
37const queryClient = new QueryClient()
38​
39class AppQueryProvider extends QueryClientProvider {
40 constructor() {
41 super()
42 this.client = queryClient
43 }
45​
46customElements.define('app-query-provider', AppQueryProvider)
47​
48class RepoStats extends LitElement {
49 private readonly repo = createQueryController(this, {
50 queryKey: ['repoData'],
51 queryFn: async () => {
52 const response = await fetch(
53 'https://api.github.com/repos/TanStack/query',
54 )
55 if (!response.ok) throw new Error('Failed to fetch repo data')
56 return response.json() as Promise<{
57 name: string
58 description: string
59 stargazers_count: number
60 }>
61 },
62 })
63​
64 render() {
65 const query = this.repo()
66​
67 if (query.isPending) return html`Loading...`
68 if (query.isError) return html`Error: ${query.error.message}`
69​
70 return html`
71 <h1>${query.data.name}</h1>
72 <p>${query.data.description}</p>
73 <strong>${query.data.stargazers_count} stars</strong>
74 `
75 }
77​
78customElements.define('repo-stats', RepoStats)
79```
80​
81Render the provider above your query consumers:
82​
83```html
84<app-query-provider>
85 <repo-stats></repo-stats>
86</app-query-provider>
87```
88​
89## Status Notes
90​
91Lit Devtools are not available yet. Use the cache APIs and the generated API reference while the adapter matures.
92​
93Start with [Installation](./installation.md), then [Quick Start](./quick-start.md), and use the [Reactive Controllers vs Hooks](./guides/reactive-controllers-vs-hooks.md) guide if you are coming from React Query.