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

Class TanstackLitQueryDemo

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

Source from the content-addressed store, hash-verified

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

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