Open History to see the last commit
1---
2id: queries
3title: Queries
4---
5β
6## Query Basics
7β
8A query is a declarative dependency on an asynchronous source of data that is tied to a **unique key**. A query can be used with any Promise based method (including GET and POST methods) to fetch data from a server. If your method modifies data on the server, we recommend using [Mutations](https://react-query.tanstack.com/docs/guides/mutations) instead.
9β
10To subscribe to a query in your components or custom hooks, call the `useQuery` hook with at least:
11β
12- A **unique key for the query**
13- A function that returns a promise that:
14 - Resolves the data, or
15 - Throws an error
16β
17```js
18import { useQuery } from 'react-query'
19β
20function App() {
21 const info = useQuery('todos', fetchTodoList)
22}
23```
24β
25The **unique key** you provide is used internally for refetching, caching, and sharing your queries throughout your application.
26β
27The query results returned by `useQuery` contains all of the information about the query that you'll need for templating and any other usage of the data:
28β
29```js
30const result = useQuery('todos', fetchTodoList)
31```
32β
33The `result` object contains a few very important states you'll need to be aware of to be productive. A query can only be in one of the following states at any given moment:
34β
35- `isLoading` or `status === 'loading'` - The query has no data and is currently fetching
36- `isError` or `status === 'error'` - The query encountered an error
37- `isSuccess` or `status === 'success'` - The query was successful and data is available
38- `isIdle` or `status === 'idle'` - The query is currently disabled (you'll learn more about this in a bit)
39β
40Beyond those primary states, more information is available depending on the state of the query:
41β
42- `error` - If the query is in an `isError` state, the error is available via the `error` property.
43- `data` - If the query is in a `success` state, the data is available via the `data` property.
44- `isFetching` - In any state, if the query is fetching at any time (including background refetching) `isFetching` will be `true`.
45β
46For **most** queries, it's usually sufficient to check for the `isLoading` state, then the `isError` state, then finally, assume that the data is available and render the successful state:
47β
48```js
49function Todos() {
50 const { isLoading, isError, data, error } = useQuery('todos', fetchTodoList)
51β
52 if (isLoading) {
53 return <span>Loading...</span>
54 }
55β
56 if (isError) {
57 return <span>Error: {error.message}</span>
58 }
59β
60 // We can assume by this point that `isSuccess === true`
61 return (
62 <ul>
63 {data.map(todo => (
64 <li key={todo.id}>{todo.title}</li>
65 ))}
66 </ul>
67 )
68}
69```
70β
71If booleans aren't your thing, you can always use the `status` state as well:
72β
73```js
74function Todos() {
75 const { status, data, error } = useQuery('todos', fetchTodoList)
76β
77 if (status === 'loading') {
78 return <span>Loading...</span>
79 }
80β
81 if (status === 'error') {
82 return <span>Error: {error.message}</span>
83 }
84β
85 // also status === 'success', but "else" logic works, too
86 return (
87 <ul>
88 {data.map(todo => (
89 <li key={todo.id}>{todo.title}</li>
90 ))}
91 </ul>
92 )
93}
94```
95## Further Reading
96β
97For an alternative way of performing status checks, have a look at the [Community Resources](../community/tkdodos-blog#4-status-checks-in-react-query).