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

overview.md

106 lines5.1 KB

1---
2id: overview
3title: Overview
4---
5​
6The `@tanstack/angular-query` package offers a 1st-class API for using TanStack Query via Angular.
7​
8If you have any feedback, please contact us at the [TanStack Discord](https://tlinz.com/discord) server or [visit this discussion](https://github.com/TanStack/query/discussions/6293) on Github.
9​
10## Supported Angular Versions
11​
12TanStack Query is compatible with Angular v20.1 and higher.
13​
14TanStack Query (FKA React Query) is often described as the missing data-fetching library for web applications, but in more technical terms, it makes **fetching, caching, synchronizing and updating server state** in your web applications a breeze.
15​
16## Motivation
17​
18Most core web frameworks **do not** come with an opinionated way of fetching or updating data in a holistic way. Because of this developers end up building either meta-frameworks which encapsulate strict opinions about data-fetching, or they invent their own ways of fetching data. This usually means cobbling together component-based state and side-effects, or using more general purpose state management libraries to store and provide asynchronous data throughout their apps.
19​
20While most traditional state management libraries are great for working with client state, they are **not so great at working with async or server state**. This is because **server state is totally different**. For starters, server state:
21​
22- Is persisted remotely in a location you may not control or own
23- Requires asynchronous APIs for fetching and updating
24- Implies shared ownership and can be changed by other people without your knowledge
25- Can potentially become "out of date" in your applications if you're not careful
26​
27Once you grasp the nature of server state in your application, **even more challenges will arise** as you go, for example:
28​
29- Caching... (possibly the hardest thing to do in programming)
30- Deduping multiple requests for the same data into a single request
31- Updating "out of date" data in the background
32- Knowing when data is "out of date"
33- Reflecting updates to data as quickly as possible
34- Performance optimizations like pagination and lazy loading data
35- Managing memory and garbage collection of server state
36- Memoizing query results with structural sharing
37​
38If you're not overwhelmed by that list, then that must mean that you've probably solved all of your server state problems already and deserve an award. However, if you are like a vast majority of people, you either have yet to tackle all or most of these challenges and we're only scratching the surface!
39​
40TanStack Query is hands down one of the _best_ libraries for managing server state. It works amazingly well **out-of-the-box, with zero-config, and can be customized** to your liking as your application grows.
41​
42TanStack Query allows you to defeat and overcome the tricky challenges and hurdles of _server state_ and control your app data before it starts to control you.
43​
44On a more technical note, TanStack Query will likely:
45​
46- Help you remove **many** lines of complicated and misunderstood code from your application and replace with just a handful of lines of Angular Query logic.
47- Make your application more maintainable and easier to build new features without worrying about wiring up new server state data sources
48- Have a direct impact on your end-users by making your application feel faster and more responsive than ever before.
49- Potentially help you save on bandwidth and increase memory performance
50​
51[//]: # 'Example'
52​
53## Enough talk, show me some code already!
54​
55In the example below, you can see TanStack Query in its most basic and simple form being used to fetch the GitHub stats for the TanStack Query GitHub project itself:
56​
57[Open in StackBlitz](https://stackblitz.com/github/TanStack/query/tree/main/examples/angular/simple)
58​
59```angular-ts
60import { ChangeDetectionStrategy, Component, inject } from '@angular/core'
61import { HttpClient } from '@angular/common/http'
62import { injectQuery } from '@tanstack/angular-query'
63import { lastValueFrom } from 'rxjs'
64​
65@Component({
66 changeDetection: ChangeDetectionStrategy.OnPush,
67 selector: 'simple-example',
68 template: `
69 @if (repoDataQuery.isPending()) {
70 Loading...
71 } @else if (query.isError()) {
72 An error has occurred: {{ query.error().message }}
73 } @else if (query.isSuccess()) {
74 @let data = query.data();
75 <h1>{{ data.name }}</h1>
76 <p>{{ data.description }}</p>
77 <strong>πŸ‘€ {{ data.subscribers_count }}</strong>
78 <strong>✨ {{ data.stargazers_count }}</strong>
79 <strong>🍴 {{ data.forks_count }}</strong>
80 }
81 `,
82})
83export class SimpleExampleComponent {
84 readonly http = inject(HttpClient)
85​
86 readonly repoDataQuery = injectQuery(() => ({
87 queryKey: ['repoData'],
88 queryFn: () =>
89 lastValueFrom(
90 this.http.get<Response>('https://api.github.com/repos/tanstack/query'),
91 ),
92 }))
94​
95interface Response {
96 name: string
97 description: string
98 subscribers_count: number
99 stargazers_count: number
100 forks_count: number
102```
103​
104## You talked me into it, so what now?
105​
106- Learn TanStack Query at your own pace with our amazingly thorough [Walkthrough Guide](./installation.md) and [API Reference](./reference/functions/injectQuery.md)