Skip to content
state management questions

State Management · 4 of 10middle~1 min

What is React Query and why is it not a traditional state manager?

#react-query#server-state

React Query

React Query (TanStack Query) is a server state manager - it handles the full lifecycle of asynchronous data: fetching, caching, synchronizing, and updating.

The problem it solves

Without React Query, server data management requires lots of manual work:

tsx
// manual approach - what you have to write yourself const [data, setData] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); useEffect(() => { setLoading(true); fetch('/api/users') .then(r => r.json()) .then(setData) .catch(setError) .finally(() => setLoading(false)); }, []); // missing: caching, deduplication, background refetch, stale data...

With React Query

tsx
const { data, isLoading, error } = useQuery({ queryKey: ['users'], queryFn: () => fetch('/api/users').then(r => r.json()), staleTime: 5 * 60 * 1000, // data is fresh for 5 minutes });

What it handles automatically

  • Caching and deduplication of identical requests.
  • Background refetching when window is refocused.
  • Automatic retries on failure.
  • Pagination and infinite scroll.
  • Optimistic updates.
  • Cache invalidation.

Mutations

tsx
const mutation = useMutation({ mutationFn: (newUser) => fetch('/api/users', { method: 'POST', body: JSON.stringify(newUser), }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['users'] }); // refetch list }, }); mutation.mutate({ name: 'Alice', email: 'alice@example.com' });

Why it's not a traditional state manager

Redux / ZustandReact Query
State typeClient stateServer state
Source of truthThe storeThe server
WritesDirectMutations + invalidation
CachingManualBuilt-in

React Query doesn't replace Zustand or Redux - it replaces the API data slices you used to put inside them. Use both: React Query for server data, Zustand for client state.

updated

did you know it?

read more on the blog

view all

related questions