Fireship · @fireship
React Query in 100 Seconds
Uploaded March 8, 2022 · Length 2:33
Summary
react query simplifies data fetching caching and synchronization in react applications.
- react query
- data fetching
- react library
- caching
- synchronization
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
react query a react library that
- 0:02
simplifies the way we fetch cache and
- 0:05
synchronize data from a server react
- 0:07
itself has no opinions about how you
- 0:09
fetch data from the front end the most
- 0:10
basic approach is to use the browser
- 0:12
fetch api when a component first mounts
- 0:15
and use effect then manage the response
- 0:16
with use state that works but becomes
- 0:18
increasingly difficult when requirements
- 0:20
like caching retries and deduping come
- 0:23
into play react query not only
- 0:24
simplifies your data fetching code but
- 0:26
also handles these complex requirements
- 0:28
out of the box for example you might
- 0:30
want to refetch data when the user
- 0:32
leaves and then comes back to the same
- 0:34
window fetch on windowfocus handles that
- 0:35
for you if you're building an infinite
- 0:37
scroll feature the use infinite query
- 0:39
hook will make your life much easier or
- 0:41
if you're writing data to the server you
- 0:42
can make changes appear instantly in the
- 0:44
ui with optimistic updates and you can
- 0:46
debug all your data fetching logic with
- 0:48
its integrated dev tools they've thought
- 0:50
of pretty much everything and it's so
- 0:51
good at what it does that it might even
- 0:53
eliminate your need for a global state
- 0:55
management solution to get started
- 0:56
install it with npm then instantiate the
- 0:58
query client and provide it somewhere in
- 1:00
your component tree now every child
- 1:02
component will be able to fetch data
- 1:04
like a pro also add the react query
- 1:06
devtools here to debug and development
- 1:08
now define a function anywhere you'd
- 1:09
like to fetch data from your server and
- 1:11
the component that consumes that data
- 1:13
add the use query hook it takes a key
- 1:15
that allows react query to manage this
- 1:17
data efficiently then the data fetching
- 1:18
function as the second argument it will
- 1:20
make the request then return the data
- 1:22
and also the status of the request if
- 1:24
it's loading we show a loading screen if
- 1:26
the request fails it will actually retry
- 1:28
the request three times and if it still
- 1:30
fails at that point the status will
- 1:31
change to error now if it was a success
- 1:33
we can render out the data in the ui and
- 1:35
that's a lot of functionality with not a
- 1:37
lot of code but you might be wondering
- 1:39
what this key is all about well now
- 1:40
let's imagine that we want to update
- 1:42
this data on the server to handle that
- 1:44
we can bring in the use mutation hook
- 1:46
when data is written to the server we
- 1:47
can hook into it with the on success
- 1:49
function and automatically invalidate
- 1:51
the query we already made based on its
- 1:53
key this tells react query to invalidate
- 1:55
and refetch the original request and we
- 1:57
can even tap into the state of this
- 1:59
process by referencing is fetching from


