Open History to see the last commit
1import { LitElement, html } from 'lit'
2import {
3 QueryClient,
4 QueryClientProvider,
5 createQueryController,
6} from '@tanstack/lit-query'
7import { fetchTodosFromServer, resetTodoApi } from './todoApi'
8import type { TodosResponse } from './todoApi'
9β
10resetTodoApi()
11β
12const queryClient = new QueryClient({
13 defaultOptions: {
14 queries: { retry: false },
15 },
16})
17β
18class BasicQueryProvider extends QueryClientProvider {
19 constructor() {
20 super()
21 this.client = queryClient
22 }
23β
24 protected override createRenderRoot(): HTMLElement | DocumentFragment {
25 return this
26 }
27}
28β
29customElements.define('basic-query-provider', BasicQueryProvider)
30β
31class BasicQueryExample extends LitElement {
32 private readonly todos = createQueryController<TodosResponse, Error>(this, {
33 queryKey: ['todos'],
34 queryFn: fetchTodosFromServer,
35 })
36β
37 protected override createRenderRoot(): HTMLElement | DocumentFragment {
38 return this
39 }
40β
41 render() {
42 const query = this.todos()
43 return html`
44 <main>
45 <h1>Basic Query Example</h1>
46 <p data-testid="basic-query-status">
47 Status: <strong>${query.status}</strong>
48 </p>
49 <button
50 data-testid="basic-refetch"
51 @click=${() => this.todos.refetch()}
52 >
53 Refetch
54 </button>
55β
56 ${
57 query.isPending
58 ? html`<p data-testid="basic-loading">Loading...</p>`
59 : null
60 }
61 ${
62 query.isError
63 ? html`<p data-testid="basic-error">
64 Error: ${String(query.error)}
65 </p>`
66 : null
67 }
68β
69 <ul data-testid="basic-todo-list">
70 ${(query.data?.items ?? []).map(
71 (todo) =>
72 html`<li data-testid="basic-todo-item">${todo.title}</li>`,
73 )}
74 </ul>
75 </main>
76 `
77 }
78}
79β
80customElements.define('basic-query-example', BasicQueryExample)
81β
82class BasicQueryRoot extends LitElement {
83 protected override createRenderRoot(): HTMLElement | DocumentFragment {
84 return this
85 }
86β
87 render() {
88 return html`
89 <basic-query-provider>
90 <basic-query-example></basic-query-example>
91 </basic-query-provider>
92 `
93 }
94}
95β
96customElements.define('basic-query-root', BasicQueryRoot)