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

quick-start.md

74 lines1.5 KB

1---
2id: quick-start
3title: Quick Start
4---
5​
6This example very briefly illustrates the 3 core concepts of React Query:
7​
8- [Queries](./guides/queries)
9- [Mutations](./guides/mutations)
10- [Query Invalidation](./guides/query-invalidation)
11​
12```js
13import {
14 useQuery,
15 useMutation,
16 useQueryClient,
17 QueryClient,
18 QueryClientProvider,
19} from 'react-query'
20import { getTodos, postTodo } from '../my-api'
21​
22// Create a client
23const queryClient = new QueryClient()
24​
25function App() {
26 return (
27 // Provide the client to your App
28 <QueryClientProvider client={queryClient}>
29 <Todos />
30 </QueryClientProvider>
31 )
33​
34function Todos() {
35 // Access the client
36 const queryClient = useQueryClient()
37​
38 // Queries
39 const query = useQuery('todos', getTodos)
40​
41 // Mutations
42 const mutation = useMutation(postTodo, {
43 onSuccess: () => {
44 // Invalidate and refetch
45 queryClient.invalidateQueries('todos')
46 },
47 })
48​
49 return (
50 <div>
51 <ul>
52 {query.data.map(todo => (
53 <li key={todo.id}>{todo.title}</li>
54 ))}
55 </ul>
56​
57 <button
58 onClick={() => {
59 mutation.mutate({
60 id: Date.now(),
61 title: 'Do Laundry',
62 })
63 }}
64 >
65 Add Todo
66 </button>
67 </div>
68 )
70​
71render(<App />, document.getElementById('root'))
72```
73​
74These three concepts make up most of the core functionality of React Query. The next sections of the documentation will go over each of these core concepts in great detail.