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

installation.md

117 lines3.3 KB

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
40 "compilerOptions": {
41 "useDefineForClassFields": false
42 }
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 }
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 }
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.