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

overview.md

101 lines4.5 KB

1---
2id: overview
3title: Overview
4---
5​
6TanStack 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.
7​
8## Motivation
9​
10Most 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.
11​
12While 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:
13​
14- Is persisted remotely in a location you do not control or own
15- Requires asynchronous APIs for fetching and updating
16- Implies shared ownership and can be changed by other people without your knowledge
17- Can potentially become "out of date" in your applications if you're not careful
18​
19Once you grasp the nature of server state in your application, **even more challenges will arise** as you go, for example:
20​
21- Caching... (possibly the hardest thing to do in programming)
22- Deduping multiple requests for the same data into a single request
23- Updating "out of date" data in the background
24- Knowing when data is "out of date"
25- Reflecting updates to data as quickly as possible
26- Performance optimizations like pagination and lazy loading data
27- Managing memory and garbage collection of server state
28- Memoizing query results with structural sharing
29​
30If 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!
31​
32React 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.
33​
34React 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.
35​
36On a more technical note, React Query will likely:
37​
38- Help you remove **many** lines of complicated and misunderstood code from your application and replace with just a handful of lines of React Query logic.
39- Make your application more maintainable and easier to build new features without worrying about wiring up new server state data sources
40- Have a direct impact on your end-users by making your application feel faster and more responsive than ever before.
41- Potentially help you save on bandwidth and increase memory performance
42​
43[//]: # 'Example'
44​
45## Enough talk, show me some code already!
46​
47In the example below, you can see React Query in its most basic and simple form being used to fetch the GitHub stats for the React Query GitHub project itself:
48​
49[Open in StackBlitz](https://stackblitz.com/github/TanStack/query/tree/main/examples/react/simple)
50​
51```tsx
52import {
53 QueryClient,
54 QueryClientProvider,
55 useQuery,
56} from '@tanstack/react-query'
57​
58const queryClient = new QueryClient()
59​
60export default function App() {
61 return (
62 <QueryClientProvider client={queryClient}>
63 <Example />
64 </QueryClientProvider>
65 )
67​
68function Example() {
69 const { isPending, error, data } = useQuery({
70 queryKey: ['repoData'],
71 queryFn: () =>
72 fetch('https://api.github.com/repos/TanStack/query').then((res) =>
73 res.json(),
74 ),
75 })
76​
77 if (isPending) return 'Loading...'
78​
79 if (error) return 'An error has occurred: ' + error.message
80​
81 return (
82 <div>
83 <h1>{data.name}</h1>
84 <p>{data.description}</p>
85 <strong>πŸ‘€ {data.subscribers_count}</strong>{' '}
86 <strong>✨ {data.stargazers_count}</strong>{' '}
87 <strong>🍴 {data.forks_count}</strong>
88 </div>
89 )
91```
92​
93[//]: # 'Example'
94[//]: # 'Materials'
95​
96## You talked me into it, so what now?
97​
98- Consider taking the official [React Query Course](https://query.gg?s=tanstack) (or buying it for your whole team!)
99- Learn React Query at your own pace with our amazingly thorough [Walkthrough Guide](./installation) and [API Reference](./reference/useQuery)
100​
101[//]: # 'Materials'