To handle server state (fetching, caching, synchronizing, and updating) in React applications, replacing manual useEffect fetching logic.
- Fetching data from REST or GraphQL APIs.
- When you need caching, deduplication, background updates, or optimistic UI.
- Setup Provider: Wrap app in
QueryClientProvider. - Create Query: Use
useQuery.- Key: Unique array
['todos', { status }]. - Fetcher: Async function returning data.
const { data, isLoading, error } = useQuery({ queryKey: [...], queryFn: fetchTodos }).
- Key: Unique array
- Create Mutation: Use
useMutationfor POST/PUT/DELETE.const mutation = useMutation({ mutationFn: addTodo }).
- Invalidate: On mutation success, invalidate query to refetch fresh data:
queryClient.invalidateQueries({ queryKey: ['todos'] }).
- Define query keys consistently.
- Handle loading and error states explicitly in the UI.
- Do not use for local synchronous state (use
useStateor Redux).
A data-fetching layer that automatically handles loading states, caching, and re-validation, reducing boilerplate significantly.