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

installation.md

109 lines3.1 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 with Lit and TanStack Query Core:
9​
10```bash
11npm i @tanstack/lit-query @tanstack/query-core lit
12```
13​
14or
15​
16```bash
17pnpm add @tanstack/lit-query @tanstack/query-core lit
18```
19​
20or
21​
22```bash
23yarn add @tanstack/lit-query @tanstack/query-core lit
24```
25​
26or
27​
28```bash
29bun add @tanstack/lit-query @tanstack/query-core lit
30```
31​
32`@tanstack/query-core` is a peer dependency of `@tanstack/lit-query`. Even though the Lit docs import user-facing APIs from `@tanstack/lit-query`, your app should install `@tanstack/query-core` explicitly.
33​
34## Requirements
35​
36Lit 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`.
37​
38TanStack Query is optimized for modern browsers:
39​
40```txt
41Chrome >= 91
42Firefox >= 90
43Edge >= 91
44Safari >= 15
45iOS >= 15
46Opera >= 77
47```
48​
49## Provider Setup
50​
51Create 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.
52​
53### Subclass Pattern
54​
55```ts
56import { QueryClient, QueryClientProvider } from '@tanstack/lit-query'
57​
58const queryClient = new QueryClient()
59​
60class AppQueryProvider extends QueryClientProvider {
61 constructor() {
62 super()
63 this.client = queryClient
64 }
66​
67customElements.define('app-query-provider', AppQueryProvider)
68```
69​
70```html
71<app-query-provider>
72 <todos-view></todos-view>
73</app-query-provider>
74```
75​
76### Direct Provider Element
77​
78You 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.
79​
80```ts
81import { LitElement, html } from 'lit'
82import { QueryClient, QueryClientProvider } from '@tanstack/lit-query'
83​
84const queryClient = new QueryClient()
85​
86customElements.define('query-client-provider', QueryClientProvider)
87​
88class AppRoot extends LitElement {
89 render() {
90 return html`
91 <query-client-provider .client=${queryClient}>
92 <todos-view></todos-view>
93 </query-client-provider>
94 `
95 }
97```
98​
99If a connected provider has no `client`, it throws. See the generated [`QueryClientProvider` reference](./reference/classes/QueryClientProvider.md) for the full contract.
100​
101## Render Roots
102​
103The 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.
104​
105Some 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.
106​
107## Devtools Status
108​
109Lit Devtools are not available yet. This is a current adapter limitation, not an installation step.