React Query Tutorial #3 - React Query Dev Tools

Net Ninja · Beginner ·🌐 Frontend Engineering ·5y ago

Key Takeaways

Demonstrates the use of React Query Dev Tools for managing asynchronous data

Full Transcript

all right then gang so as well as react query we can install another package which works with react query to help us when using the library and that package is called query dev tools and it gives us a development console in the browser that shows what's happening with our queries behind the scenes so it's really helpful so i'm going to install that separate package now first of all i'm going to open up a new terminal so i don't have to quit out of this process and i'm going to say npm iphone install save this to dependencies and it's called react query dev tools like so so hit enter and install that package first of all okay so now we have that let's close this down and import it so i'm going to go to the app root component over here and at the top i'm going to paste in this import where i import react query dev tools from react query dev tools so how do we use this down here well we need to render these dev tools to the dom and i want to do it adjacent to this app right here not inside the app so i could down here say i want to use this react query dev tools component like so and also i'm going to add a prop called initial is open and set that equal to false so by default when we open up the app in a browser we don't just see this giant box on the screen as well this dev tools we have to open it up okay so now we've done that the next thing we need to do is surround all of this in some other element because the root of this jsx has to be one element and currently we have two top level elements so let's just surround all of this with a fragment so empty angle brackets like that and this is basically an empty element just so we have one root level element and that is now fine so if i save this now and check it out in the browser then we see this little thing down here in the bottom right and that's a switch to open up these dev tools and it looks like this so you can see the queries that we have and we can already see we have one called planets that it's made in the background to grab these planets so if we click on that we can see information about the planet's request so first of all you're going to see right here we can refetch if we want to to get fresh data and also down here we can see the data itself and we can see all of the results we have in that data we also have information about the query itself and we also have some config as well so we're going to look at that in a minute that config but also over here you can see we have these little icons right here now at the minute it's saying we have one stale query meaning that already this data could be out of date it could be old because we fetched it a few seconds ago essentially and the data could have updated since then so when we first fetch the data it's fresh for a fraction of a second and then it becomes stale straight away and you're going to see that if i try to refresh keep your eye on this fresh icon right here it goes green for a millisecond and then it becomes stale again so react query uses this under the hood to decide when to refetch data in the background to keep it fresh and it does this for example when we refocus on the window for all stale data if data is stale it will try to refetch it in the background to see if we have some updated data for us so if i go away and then come back over here to refocus you see that blink again that was us refocusing the window and it re-fetched the data in the background to see if we had any updated data that we could render to the dom so again that was only fresh for a millisecond just watch this again fresh for a millisecond so it's doing that refresh in the background all the time to make sure we have up-to-date data but we're always seeing cache data in the browser anyway so we don't have to wait for anything to see which is nice so we can control how long data is fresh for in the config of react query and that's what this config option right here is all about so the stale time right here is zero meaning that as soon as we get data it's not even fresh for a millisecond it's automatically stale but if we want to wait two seconds for example or 2000 milliseconds we can specify that in a config object so how do we do that well when we use the query or the use query hook which is in planets if i can find it right here so when we use this right here we can also pass as a third argument an object which represents the config so inside here for example i could set the scale time so still time and set that to be 2 000 milliseconds and now a query will remain fresh for two seconds before it becomes stale so if i save it and come over here now you see it's fresh for longer then it becomes stale if i change this to like 5000 then it's fresh for five seconds now if i go to another browser and then come back to refocus it tries to get that but if i go quickly within that five seconds it doesn't do another refetch because it's still fresh it only re-fetches stale data so there are other things we can change in the config as well if i just open this up we can see we have this retry thing right here and that is how many times failed queries should be retried before giving an error the default for this is three times and we saw that earlier on in this series we also have the cache time right here and that's how long stale queries are cached for before they're disposed of now the default is 300 000 milliseconds which is five minutes and that means if we go to another component and then back again it will use cached data and it will be really quick to load folders five minutes since the fetch so let's try setting this to 10 milliseconds and see what that looks like so i'm going to come down here and say cache time and set that to 10 milliseconds and in fact let's change this back to zero to get the default behavior again and go back over here and notice now every time we go to these different pages it's refetching and that's only for the planets not the people because we're using this in the planets component right here so every time we go to planets we see loading data now before before we had this we didn't see that because we were using cached data so if we go to refresh first time we see loading but then if we go back to planets we don't see it because it's using cached data does that make sense now there's also other properties we can add to this such as refetch or mount and that's true by default meaning we refetch data in the background every time a new component mounts which needs the data to see if it's updated there's also properties which have functions as values that can fire at different stages of the request for example we can use the on success property right here to fire a function whenever we have a success status so let me just do a quick one right here quick arrow function which says console.log and inside that data fetched with no problemo okay so if we save that now we should see that in the console if we open it up so data fetched with no problem oh we could also have a function for on error as well if we wanted to and we could use these functions to maybe show notifications to the users when either new data has been added or something goes wrong and there's more options you can add to this as well so for a full list definitely check out the docs they're going to be in the description down below but that my friends in a nutshell is the react query dev tools and the config option right here so next up we're going to see how to pass query variables to our use query hook

Original Description

Hey all - in this React Query tutorial we'll see how to use theReact Quer dev tools to help us see what's happening under thood when we use React Query to manage our asynchronous data. 🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join ---------------------------------------- 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Course Files: + https://github.com/iamshaunjp/react-query-tutorial 🐱‍💻 🐱‍💻 Other Related Free Courses: + React Tutorial - https://www.youtube.com/playlist?list=PL4cUxeGkcC9ij8CfkAY2RAGb-tmkNwQHG + React Hooks (& Context) Tutorial - https://www.youtube.com/playlist?list=PL4cUxeGkcC9hNokByJilPg5g9m2APUePI Download node.js - https://nodejs.org/en/ Get VS Code - https://code.visualstudio.com/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Prepare for a frontend developer interview with Capgemini by reviewing JavaScript fundamentals and practicing common interview questions
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with 10 essential tools for modern web app development
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with top 10 developer tools in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
US frontend engineer hiring demand peaked in 2022 and remains flat-depressed in 2026, contrary to common assumptions
Dev.to AI
Up next
How to Open KIT Files (CodeKit Project)
File Extension Geeks
Watch →