Open History to see the last commit
1<div align="center">
2 <picture>
3 <source
4 media="(prefers-color-scheme: dark)"
5 srcset="https://tanstack.com/api/readme/query.png?framework=lit&theme=dark"
6 />
7 <source
8 media="(prefers-color-scheme: light)"
9 srcset="https://tanstack.com/api/readme/query.png?framework=lit"
10 />
11 <img
12 src="https://tanstack.com/api/readme/query.png?framework=lit"
13 alt="TanStack Lit Query"
14 width="900"
15 />
16 </picture>
17</div>
18# @tanstack/lit-query
19β
20Lit adapter for `@tanstack/query-core` using Lit reactive controllers.
21β
22This package is currently experimental and v0.1. Pin exact versions if you use
23it in production while the API is early.
24β
25## Install
26β
27```bash
28npm install @tanstack/lit-query @tanstack/query-core lit
29```
30β
31For local development in this repository:
32β
33```bash
34pnpm install
35pnpm --dir packages/lit-query run build
36```
37β
38## Quick Start
39β
40```ts
41import { LitElement, html } from 'lit'
42import {
43 QueryClient,
44 QueryClientProvider,
45 createQueryController,
46} from '@tanstack/lit-query'
47β
48const client = new QueryClient({
49 defaultOptions: { queries: { retry: false } },
50})
51β
52class AppProvider extends QueryClientProvider {
53 constructor() {
54 super()
55 this.client = client
56 }
57}
58customElements.define('app-provider', AppProvider)
59β
60class UsersView extends LitElement {
61 private readonly users = createQueryController(this, {
62 queryKey: ['users'],
63 queryFn: async () => {
64 const response = await fetch('/api/users')
65 return response.json() as Promise<Array<{ id: string; name: string }>>
66 },
67 })
68β
69 render() {
70 const query = this.users()
71 if (query.isPending) return html`Loading...`
72 if (query.isError) return html`Error`
73 return html`<ul>
74 ${query.data?.map((u) => html`<li>${u.name}</li>`)}
75 </ul>`
76 }
77}
78customElements.define('users-view', UsersView)
79```
80β
81## API Surface
82β
83- `QueryClientProvider`, `useQueryClient`, `resolveQueryClient`
84- `createQueryController`
85- `createMutationController`
86- `createInfiniteQueryController`
87- `createQueriesController`
88- `useIsFetching`, `useIsMutating`, `useMutationState`
89- `queryOptions`, `infiniteQueryOptions`, `mutationOptions`
90β
91## Runnable Examples
92β
93This repo includes runnable Lit examples under the top-level `examples/lit`
94directory so they can be surfaced in the docs:
95β
96- `examples/lit/basic`: Vite Lit app covering query and mutation primitives.
97- `examples/lit/pagination`: pagination, prefetching, optimistic updates, and error recovery.
98- `examples/lit/ssr`: Lit SSR render, dehydrate, and hydrate flow.
99β
100Run an example from the repo root:
101β
102```bash
103pnpm --dir examples/lit/basic run dev
104pnpm --dir examples/lit/pagination run dev
105pnpm --dir examples/lit/ssr run dev
106```
107β
108Open:
109β
110- `http://127.0.0.1:4173/` (basic example)
111- `http://127.0.0.1:4183/` (pagination example app)
112- `http://127.0.0.1:4174/` (SSR example app)
113β
114Use a different port (optional):
115β
116```bash
117DEMO_PORT=4180 pnpm --dir examples/lit/basic run dev
118PAGINATION_DEMO_PORT=4181 PAGINATION_API_PORT=4182 pnpm --dir examples/lit/pagination run dev
119SSR_PORT=4180 pnpm --dir examples/lit/ssr run dev
120SSR_HOST=0.0.0.0 pnpm --dir examples/lit/ssr run dev
121```
122β
123## Integration Smoke
124β
125For the framework build smoke used in CI:
126β
127```bash
128pnpm --dir integrations/lit-vite run build
129```
130β
131## Quality Gates
132β
133From the repository root:
134β
135```bash
136pnpm --dir packages/lit-query run test:types
137pnpm --dir packages/lit-query run test:lib
138pnpm --dir packages/lit-query run build
139pnpm --dir packages/lit-query run test:build
140```
141β
142## License
143β
144MIT