Open History to see the last commit
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 )
32}
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 )
69}
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.