Open History to see the last commit
1---
2id: installation
3title: Installation
4---
5β
6> IMPORTANT: The Lit adapter is currently experimental and v0.1. Pin exact versions if you need extra release stability while the API is early.
7β
8Install Lit Query:
9β
10```sh
11npm i @tanstack/lit-query
12```
13β
14or
15β
16```sh
17pnpm add @tanstack/lit-query
18```
19β
20or
21β
22```sh
23yarn add @tanstack/lit-query
24```
25β
26or
27β
28```sh
29bun add @tanstack/lit-query
30```
31β
32## Requirements
33β
34Lit Query is intended for Lit 2.8 and newer, including Lit 3. It uses Lit reactive controllers and Lit context, so query consumers should be `ReactiveControllerHost` instances such as `LitElement`.
35β
36The snippets in these docs declare reactive properties with `static properties` and initialize them as class fields. Class fields that use the native `[[Define]]` semantics shadow the accessors Lit uses to detect changes, so with TypeScript set `useDefineForClassFields` to `false`, as the runnable examples do. See [Avoiding issues with class fields](https://lit.dev/docs/components/properties/#avoiding-issues-with-class-fields) in the Lit docs for more details.
37β
38```json
39{
40 "compilerOptions": {
41 "useDefineForClassFields": false
42 }
43}
44```
45β
46TanStack Query is optimized for modern browsers:
47β
48```txt
49Chrome >= 91
50Firefox >= 90
51Edge >= 91
52Safari >= 15
53iOS >= 15
54Opera >= 77
55```
56β
57## Provider Setup
58β
59Create a `QueryClient`, provide it with `QueryClientProvider`, and register your custom element. The package exports the provider class but does not call `customElements.define` for you.
60β
61### Subclass Pattern
62β
63```ts
64import { QueryClient, QueryClientProvider } from '@tanstack/lit-query'
65β
66const queryClient = new QueryClient()
67β
68class AppQueryProvider extends QueryClientProvider {
69 constructor() {
70 super()
71 this.client = queryClient
72 }
73}
74β
75customElements.define('app-query-provider', AppQueryProvider)
76```
77β
78```html
79<app-query-provider>
80 <todos-view></todos-view>
81</app-query-provider>
82```
83β
84### Direct Provider Element
85β
86You can register the provider class directly and bind its `client` property from a Lit template. The dot is important: `.client=${queryClient}` is a property binding, not an HTML attribute.
87β
88```ts
89import { QueryClient, QueryClientProvider } from '@tanstack/lit-query'
90import { LitElement, html } from 'lit'
91β
92const queryClient = new QueryClient()
93β
94customElements.define('query-client-provider', QueryClientProvider)
95β
96class AppRoot extends LitElement {
97 render() {
98 return html`
99 <query-client-provider .client=${queryClient}>
100 <todos-view></todos-view>
101 </query-client-provider>
102 `
103 }
104}
105```
106β
107If a connected provider has no `client`, it throws. See the generated [`QueryClientProvider` reference](./reference/classes/QueryClientProvider.md) for the full contract.
108β
109## Render Roots
110β
111The snippets in these docs use Lit's default shadow DOM. Lit Query controllers and `QueryClientProvider` work with shadow DOM and light DOM because they use the host lifecycle and Lit context, not global selectors.
112β
113Some runnable examples override `createRenderRoot()` and return `this` so their demo markup stays in light DOM for shared page styles and test selectors. That override is not required for Lit Query. Use light DOM only when your app has a separate reason to expose a component's internal markup to global CSS, tests, or non-shadow-DOM integration code.
114β
115## Devtools Status
116β
117Lit Devtools are not available yet. This is a current adapter limitation, not an installation step.