Language: JavaScript
Web
React Query was created by Tanner Linsley to solve common challenges in managing server state in React apps, such as caching, background updates, and synchronization. It allows developers to focus on building UI while React Query manages the complexity of data fetching and caching efficiently.
React Query is a powerful data-fetching library for React that simplifies fetching, caching, synchronizing, and updating server state in your React applications.
npm install @tanstack/react-queryyarn add @tanstack/react-queryReact Query provides hooks like `useQuery`, `useMutation`, and `useInfiniteQuery` to fetch and mutate data. It handles caching, background refetching, pagination, and stale data management automatically.
import { useQuery } from '@tanstack/react-query';
import axios from 'axios';
function App() {
const { data, error, isLoading } = useQuery(['todos'], () => axios.get('/api/todos').then(res => res.data));
if (isLoading) return <div>Loading...</div>;
if (error) return <div>Error!</div>;
return (
<ul>
{data.map(todo => <li key={todo.id}>{todo.title}</li>)}
</ul>
);
}Fetches a list of todos from an API and automatically manages loading, error, and cached states.
import { useMutation, useQueryClient } from '@tanstack/react-query';
import axios from 'axios';
function AddTodo() {
const queryClient = useQueryClient();
const mutation = useMutation(newTodo => axios.post('/api/todos', newTodo), {
onSuccess: () => queryClient.invalidateQueries(['todos'])
});
return <button onClick={() => mutation.mutate({ title: 'New Todo' })}>Add Todo</button>;
}Adds a new todo item and invalidates the todos query to refresh cached data.
const { data, isFetching, fetchNextPage } = useInfiniteQuery(['todos'], fetchTodos, {
getNextPageParam: lastPage => lastPage.nextCursor
});Demonstrates fetching data in pages and automatically handling pagination using `useInfiniteQuery`.
const queryClient = useQueryClient();
queryClient.invalidateQueries(['todos']);Manually invalidates cached queries to refetch fresh data from the server.
queryClient.prefetchQuery(['todos'], fetchTodos);Prefetches data in the background so it is ready when a component mounts.
Use `useQuery` for fetching data and `useMutation` for modifying data.
Leverage query keys for caching and invalidation granularity.
Use `staleTime` and `cacheTime` wisely to balance performance and freshness.
Prefetch data when possible for faster UI transitions.
Combine with `useQueryClient` for advanced cache manipulation and query invalidation.