This example very briefly illustrates the 3 core concepts of React Query:
import { useQuery, useMutation, useQueryClient, QueryCache, QueryClient, QueryClientProvider } from 'react-query'import { getTodos, postTodo } from '../my-api'const cache = new QueryCache()const client = new QueryClient({ cache })function App() {return (<QueryClientProvider client={client}><Todos /></QueryClientProvider>);}function Todos() {// Cacheconst client = useQueryClient()// Queriesconst todosQuery = useQuery('todos', getTodos)// Mutationsconst [addTodo] = useMutation(postTodo, {onSuccess: () => {// Query Refetchclient.refetchQueries('todos')},})return (<div><ul>{todosQuery.data.map(todo => (<li key={todo.id}>{todo.title}</li>))}</ul><buttononClick={() =>addTodo({id: Date.now()title: 'Do Laundry',})}>Add Todo</button></div>)}render(<App />, document.getElementById('root'));
These 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.
The latest TanStack news, articles, and resources, sent to your inbox.