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

quick-start.md

122 lines2.9 KB

1---
2id: quick-start
3title: Quick Start
4---
5​
6> IMPORTANT: This library is currently in an experimental stage. This means that breaking changes will happen in minor AND patch releases. Upgrade carefully. If you use this in production while in experimental stage, please lock your version to a patch-level version to avoid unexpected breaking changes.
7​
8[//]: # 'Example'
9​
10If you're looking for a fully functioning example, please have a look at our [basic codesandbox example](../examples/basic)
11​
12### Provide the client to your App
13​
14```ts
15import { provideHttpClient } from '@angular/common/http'
16import {
17 provideAngularQuery,
18 QueryClient,
19} from '@tanstack/angular-query-experimental'
20​
21bootstrapApplication(AppComponent, {
22 providers: [provideHttpClient(), provideAngularQuery(new QueryClient())],
23})
24```
25​
26or in a NgModule-based app
27​
28```ts
29import { provideHttpClient } from '@angular/common/http'
30import {
31 provideAngularQuery,
32 QueryClient,
33} from '@tanstack/angular-query-experimental'
34​
35@NgModule({
36 declarations: [AppComponent],
37 imports: [BrowserModule],
38 providers: [provideAngularQuery(new QueryClient())],
39 bootstrap: [AppComponent],
40})
41export class AppModule {}
42```
43​
44### Component with query and mutation
45​
46```angular-ts
47import { Component, Injectable, inject } from '@angular/core'
48import { HttpClient } from '@angular/common/http'
49import { lastValueFrom } from 'rxjs'
50​
51import {
52 injectMutation,
53 injectQuery,
54 injectQueryClient,
55} from '@tanstack/angular-query-experimental'
56​
57@Component({
58 standalone: true,
59 template: `
60 <div>
61 <button (click)="onAddTodo()">Add Todo</button>
62​
63 <ul>
64 @for (todo of query.data(); track todo.title) {
65 <li>{{ todo.title }}</li>
66 }
67 </ul>
68 </div>
69 `,
70})
71export class TodosComponent {
72 todoService = inject(TodoService)
73 queryClient = injectQueryClient()
74​
75 query = injectQuery(() => ({
76 queryKey: ['todos'],
77 queryFn: () => this.todoService.getTodos(),
78 }))
79​
80 mutation = injectMutation((client) => ({
81 mutationFn: (todo: Todo) => this.todoService.addTodo(todo),
82 onSuccess: () => {
83 // Invalidate and refetch by using the client directly
84 client.invalidateQueries({ queryKey: ['todos'] })
85​
86 // OR use the queryClient that is injected into the component
87 // this.queryClient.invalidateQueries({ queryKey: ['todos'] })
88 },
89 }))
90​
91 onAddTodo() {
92 this.mutation.mutate({
93 id: Date.now().toString(),
94 title: 'Do Laundry',
95 })
96 }
98​
99@Injectable({ providedIn: 'root' })
100export class TodoService {
101 private http = inject(HttpClient)
102​
103 getTodos(): Promise<Todo[]> {
104 return lastValueFrom(
105 this.http.get<Todo[]>('https://jsonplaceholder.typicode.com/todos'),
106 )
107 }
108​
109 addTodo(todo: Todo): Promise<Todo> {
110 return lastValueFrom(
111 this.http.post<Todo>('https://jsonplaceholder.typicode.com/todos', todo),
112 )
113 }
115​
116interface Todo {
117 id: string
118 title: string
120```
121​
122[//]: # 'Example'