React Query maintainer TkDodo has a series of blog posts about using and working with the library. Some articles show general best practices, but most have an opinionated point of view.
An advanced introduction to React Query, showing practical tips that go beyond the docs. It covers explaining the defaults (
staleTime
vs.cacheTime
), concepts like keeping server and client state separate, handling dependencies and creating custom hooks, as well as outlining why theenabled
option is very powerful. Read more...
Learn the possibilities to perform the quite common and important task of transforming your data with React Query. From transforming in the
queryFn
to using theselect
option, this article outlines the pros and cons of all the different approaches. Read more...
Let's take a look at what you can do when your component re-renders too often when using React Query. The library is already pretty optimized, but there are still some opt-in features (like
tracked queries
) that you can use to avoid theisFetching
transition. We're also looking into whatstructural sharing
refers to. Read more...
We usually check for
isLoading
first before checking forisError
, but sometimes, checking ifdata
is available should be the first thing to do. This article shows how the wrong status check order can negatively impact user experience. Read more...
The docs already cover pretty well what you need to do to get started when testing React Query. This article shows some additional tips (like turning off
retries
or silencing theconsole
) you might want to follow when testing custom hooks or components using them. It also links to an example repository with tests for success and error states, powered bymock-service-worker
. Read more...
Since React Query is written in TypeScript, it has great support for it. This blog post explains the various Generics, how you can leverage type inference to avoid having to explicitly type
useQuery
and friends, what to do withunknown
errors, how type narrowing works and more! Read more...
A step-by-step guide on how to make real-time notifications work with React Query, with either event-based subscriptions or pushing full data directly to the client. Applicable to anything from the browser native WebSocket API over Firebase and even GraphQL subscriptions. Read more...
Most examples just use a simple String or Array Query Key, but how do you organize your keys effectively once your app grows past a todo list? This article shows how co-location and Query Key Factories can make life easier. Read more...
Placeholder and Initial Data are two similar yet different concepts for synchronously showing data instead of a loading spinner to improve an application's UX. This blog post compares the two and outlines the scenarios where each one shines. Read more...
React Query doesn't fetch any data for you - it's a data synchronization tool that excels when used for server state. This article has everything you need to know to make React Query your single source of truth state manager for your async state. You'll learn how to let React Query do it's magic and why customizing
staleTime
might be all you need. Read more...
Handling errors is an integral part of working with asynchronous data, especially data fetching. We have to face it: Not all requests will be successful, and not all Promises will be fulfilled. This blog post describes various ways of coping with errors in React Query, such as the error property, using Error Boundaries or onError callbacks, so that you can prepare your application for the cases when "Something went wrong". Read more...
Mutations are the important, second part necessary to work with server data - for situations where you need to update it. This blog post covers what mutations are and how they are different from queries. You'll learn the difference between
mutate
andmutateAsync
as well as how you can tie queries and mutations together. Read more...
The best JavaScript newsletter! Delivered every Monday to over 76,000 devs.