Fireship · @fireship

React Query in 100 Seconds

Uploaded March 8, 2022 · Length 2:33

React Query in 100 Seconds

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.

  1. 0:00

    react query a react library that

  2. 0:02

    simplifies the way we fetch cache and

  3. 0:05

    synchronize data from a server react

  4. 0:07

    itself has no opinions about how you

  5. 0:09

    fetch data from the front end the most

  6. 0:10

    basic approach is to use the browser

  7. 0:12

    fetch api when a component first mounts

  8. 0:15

    and use effect then manage the response

  9. 0:16

    with use state that works but becomes

  10. 0:18

    increasingly difficult when requirements

  11. 0:20

    like caching retries and deduping come

  12. 0:23

    into play react query not only

  13. 0:24

    simplifies your data fetching code but

  14. 0:26

    also handles these complex requirements

  15. 0:28

    out of the box for example you might

  16. 0:30

    want to refetch data when the user

  17. 0:32

    leaves and then comes back to the same

  18. 0:34

    window fetch on windowfocus handles that

  19. 0:35

    for you if you're building an infinite

  20. 0:37

    scroll feature the use infinite query

  21. 0:39

    hook will make your life much easier or

  22. 0:41

    if you're writing data to the server you

  23. 0:42

    can make changes appear instantly in the

  24. 0:44

    ui with optimistic updates and you can

  25. 0:46

    debug all your data fetching logic with

  26. 0:48

    its integrated dev tools they've thought

  27. 0:50

    of pretty much everything and it's so

  28. 0:51

    good at what it does that it might even

  29. 0:53

    eliminate your need for a global state

  30. 0:55

    management solution to get started

  31. 0:56

    install it with npm then instantiate the

  32. 0:58

    query client and provide it somewhere in

  33. 1:00

    your component tree now every child

  34. 1:02

    component will be able to fetch data

  35. 1:04

    like a pro also add the react query

  36. 1:06

    devtools here to debug and development

  37. 1:08

    now define a function anywhere you'd

  38. 1:09

    like to fetch data from your server and

  39. 1:11

    the component that consumes that data

  40. 1:13

    add the use query hook it takes a key

  41. 1:15

    that allows react query to manage this

  42. 1:17

    data efficiently then the data fetching

  43. 1:18

    function as the second argument it will

  44. 1:20

    make the request then return the data

  45. 1:22

    and also the status of the request if

  46. 1:24

    it's loading we show a loading screen if

  47. 1:26

    the request fails it will actually retry

  48. 1:28

    the request three times and if it still

  49. 1:30

    fails at that point the status will

  50. 1:31

    change to error now if it was a success

  51. 1:33

    we can render out the data in the ui and

  52. 1:35

    that's a lot of functionality with not a

  53. 1:37

    lot of code but you might be wondering

  54. 1:39

    what this key is all about well now

  55. 1:40

    let's imagine that we want to update

  56. 1:42

    this data on the server to handle that

  57. 1:44

    we can bring in the use mutation hook

  58. 1:46

    when data is written to the server we

  59. 1:47

    can hook into it with the on success

  60. 1:49

    function and automatically invalidate

  61. 1:51

    the query we already made based on its

  62. 1:53

    key this tells react query to invalidate

  63. 1:55

    and refetch the original request and we

  64. 1:57

    can even tap into the state of this

  65. 1:59

    process by referencing is fetching from