MCPcopy Create free account
hub / github.com/TanStack/query / TanstackLitQueryDemo

Class TanstackLitQueryDemo

examples/lit/basic/src/main.ts:44–254  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

42customElements.define('demo-query-provider', DemoQueryProvider)
43
44class TanstackLitQueryDemo extends LitElement {
45 static properties = {
46 nextTodoTitle: { state: true },
47 cacheSeedCount: { state: true },
48 }
49
50 private nextTodoTitle = 'Add mutation assertion'
51 private cacheSeedCount = 0
52
53 private readonly todosQuery = createQueryController(this, todosQueries.list())
54
55 private readonly createTodoMutation = createMutationController(this, {
56 ...todosMutations.create(),
57 onSuccess: (createdTodo) => {
58 demoQueryClient.setQueryData<TodosResponse>(
59 todosQueries.list().queryKey,
60 (existing) => {
61 if (!existing) {
62 return {
63 items: [createdTodo],
64 requestCount: 0,
65 source: 'cache',
66 }
67 }
68
69 return {
70 items: [...existing.items, createdTodo],
71 requestCount: existing.requestCount,
72 source: 'cache',
73 }
74 },
75 )
76 },
77 })
78
79 private readonly isFetching = useIsFetching(this, {
80 queryKey: todosQueries.all(),
81 })
82
83 private readonly isMutating = useIsMutating(this, {
84 mutationKey: todosMutations.create().mutationKey,
85 })
86
87 protected override createRenderRoot(): HTMLElement | DocumentFragment {
88 return this
89 }
90
91 private onTitleInput(event: Event): void {
92 const target = event.target as HTMLInputElement
93 this.nextTodoTitle = target.value
94 }
95
96 private addTodo(): void {
97 const title = this.nextTodoTitle.trim()
98 if (!title) {
99 return
100 }
101

Callers

nothing calls this directly

Calls 5

createQueryControllerFunction · 0.90
createMutationControllerFunction · 0.90
useIsFetchingFunction · 0.90
useIsMutatingFunction · 0.90
setQueryDataMethod · 0.45

Tested by

no test coverage detected