Welcome to the React show. Do you prefer to have tons of extra network requests and re-renders even when the underlying data hasn't changed? No? Well, if you use React, I think you should be using a query caching library like React Query or SWR. And we're going to tell you why and how best to use them. You were born a social animal in a world that was meant to be less measured, less exact. And now you sit with dancing photons blasting away your eyes as your muscles atrophy and your social skills lapse. In a world where everything is measured and exact. You emerge from your cave that you call an office to find yourself torn away by the currents of the journey. Of the journey, we are all bound together. Whether you're just learning to write code, or you're a 50-year expert that knows APL just as well as TypeScript, we're all on the same journey together. So join us on the React Show as we explore how to make better programs, better teams, better companies, and a better world together. The React Show is a weekly podcast brought to you from occupied Miwak and Pohatan Territory, focused on learning more about React, programming in general, and the intersection of programming and the rest of the world. Come join us on this journey. Thank you for joining us on episode 34. It's not exactly React, but I have been caching bike parts lately. The caching wasn't really intentional, um, but but if it will help, I guess, with latency, right? Um, and and actually the the funny thing is, you know, even though like so there is definitely a cost of caching, right? Like you gotta have the extra space for the bike parts and you know, organize and label, like you gotta be able to find it, right? But it can really pay off. Like um in this last case, I was working on my bike, like some handlebar stuff, and I made a mistake. But I didn't have to make a long trip by skateboard to the shop because I happened to have the cache of the part I needed on hand. Brilliant, right? Anyways, definitely an order of magnitude faster. Caching for the win, hey? Well, yeah, so that's my little story about caching, but anyways, Austin, what about you? Are you cashing anything lately?
SPEAKER_00Yeah, when the um first toilet paper crisis happened, uh, when when COVID started. Uh I did not I did not buy a ton of toilet paper, uh, but I did like order a meals ready to eat box from like a military surplus supplier, and it was like a thousand bucks, and it's like a whole year's worth of MREs that won't go bad for like 70 years or something like that. So I have a a pretty sizable cache down in the basement just in case.
SPEAKER_01I totally thought I knew where this was going, and it was totally wrong.
SPEAKER_00Well, actually, yeah, my we used to my dad used to run a uh we used to manage a swimming pool down the road, and uh one time there was like an extra shipment, like we we ordered toilet paper for the pool, and there were like for some we ordered like one crate of them uh to put in the back of the the storage place, and for some reason like three crates came, so we like took the two extra crates home, but it was like that terrible sandpaper toilet paper, and it was like it was the worst for like a year.
SPEAKER_01It was just you you guys went through two crates in a year? It must have been bad toilet paper.
SPEAKER_00This was when I was this was when I was oh yeah, you know, that stuff that's like single ply and like quadruple layer it.
SPEAKER_01Yeah, yeah.
SPEAKER_00And I also did like uh have fun like in my earlier pyro days as a kid, like catching them on fire, starting fires with these toilet paper rolls and stuff like that.
SPEAKER_01So never I did we did plenty of lighting things that fire. I don't think we ever did toilet paper though.
SPEAKER_00Yeah, one time I took um I took like a bunch of like butane and I like got uh duct tape and I like duct taped the bottom of one of the toilet paper rolls and then I like filled it up with butane and made like a toilet paper bomb. Uh that was pretty fun. It was I like I took the toy I took like uh a couple of sheets, like uh probably a hundred twenty hundred sheets, and I like twisted them up and made like a little like a line that I lit and like stuck it into that. It was pretty cool.
SPEAKER_01It actually sounds a lot like what I did as a kid, to be honest. Yeah. Yeah, we we definitely um went down that path in some regards. So yeah, we're with All Creek, uh clearly an organization focused on I don't know, toilet paper and blowing things up. But when we're not doing that, uh we talk about React and you know, uh focus on React education. Um and we're all glad to have you join us today. Um, yeah, so like always, I'm your host with my amazing co-host Austin. Welcome to the show, Austin.
SPEAKER_00Howdy, hey.
SPEAKER_01I should I should have been like with my amazing specialist in lighting toilet paper on fire, co-host Austin. Welcome to the show.
SPEAKER_00Could you give me a LinkedIn uh recommendation on that one?
SPEAKER_01If I was on LinkedIn, I would like to.
SPEAKER_00Oh, right, I forgot.
SPEAKER_01Um, yeah, so um I I think we can probably move on to something more substantive here. Um, yeah, if if you know, like uh like I would say the owls on Owl Creek, you know, they they're probably you know caching, right? So it's time to get cash actually, now that I think about it, owls don't cache, do they, Austin? Do they cache anything? I can't think of anything.
SPEAKER_00Yeah, I don't after they regurgitate the bones and fur and stuff, I don't think they do anything with that.
SPEAKER_01Yeah, so I think maybe while owls aren't caching anything, um, we're just gonna talk about caching anyways. So it's time to move on. Um yeah, so what we have planned for today is is first we're gonna take a look at um how how a lot of React applications interact with data, like with endpoints and and how that usually shakes out and a lot of the problems with that. Um and some of the the things that we've run into, um, you know, whether it's like making requests over and over again, even when it's not needed, or like chaining requests, or not making requests in the right place. So we're gonna take a look at all of those kinds of things. Um and then we're gonna actually go on to look at, hey, you know, what can we do about this? What can a caching library do for us? You know, how does that work? How do you integrate it? Um, that kind of thing. Uh so yeah, we're gonna um take a look at that and also maybe some downsides and and you know, just overall in general, you know, take a look at um how you can best access and manipulate data within a React application, like how it will fit into the React paradigm, you know, well. Um yeah, so that's that's uh that's the plan. Um I think uh yeah, we'll get into it. So an example um that uh I have run into that I think was really fascinating is in fact actually I think this is what happens a lot, is um you need to make a request to your back end and you put that in a hook. So you can just use that, you know, anywhere in your app where you need that data. Um well, what often ends up happening is you'll need to make requests based on other data. So you have to make a request to get like maybe the stores that you sell to, and then after that, you want to maybe make a request to get data about a specific store or whatever. So you need to fetch the stores first, then you need to fetch this other data. And when you use this really naive method of putting your fetching into uh hooks, um it's really clean and it it works like well from like a you know, like it works and functions, and it's not gonna be necessarily buggy, but you end up with these like chained requests that happen all the time. Like maybe in this case your stores hook, maybe you only need to fetch it like once when the user loads, but you're including it on every page because it's like a dependent thing that all your other queries depend on, and you're not like necessarily caching that data. So you're making that request, then you're making your other requests, and you're doing this on every page, and it's entirely unnecessary. Um, which yeah, is in my opinion, like it works, but it's definitely subpar. Um, I don't know, so uh Austin, I know you have had some similar experiences, but um, were there any like um other cases you you could think of that um you saw sort of like a subpar data fetching experience because of like the simple hook method?
SPEAKER_00Yeah, and the in one of the main projects that we're working on, like you're mentioning like getting the getting these uh stores or getting some other thing that then calls another. Yeah, I've I've experienced this multiple times. Um I don't I actually just started this past year using the hooks paradigm to begin with my uh to begin like fetching data and so I don't have a whole lot of experience with it uh to be honest per se, you using these hooks methods until we got into this project. But I do I really did like the paradigm. It was really cool, but noticed that it was there there were a lot of problems coming through with these these chain requests where we would have to where data did depend on each other.
SPEAKER_01Yeah, yeah. Um so I think and I think that's something like basically anyone that's used React has run into, right? Like it it just is you know it is how it happens naturally, I think. So um yeah, so uh the the other thing is too, like when this um when you use this sort of naive system, um what if you are making a request and you get the same data back? How is that gonna impact your React application? Um the the data that like the JSON or whatever that you get back, it's gonna end up being a different um you know data. So React is gonna see that as needing to re-render if you're storing that in state or something. So even if the data hasn't changed, you're just constantly re-rendering your app for no reason. Which is, yeah, obviously subpar. Um and so yeah, those are sort of the main issues that I've seen in in this situation. Um, but we also have another one that I think doesn't get talked about as much and that a lot of people don't think about is so let me ask you this, Austin. If you're if you're doing this like naive system where you just have a hook that fetches data, where are you gonna fetch this data? Are you gonna, you know, like I I'll just ask it straight up, where where are you gonna put this hook?
SPEAKER_00Well, as we know, hooks cannot be conditionally called. So it usually I put these kind of hooks up at the very top of the component. And um I don't think that ordering them really has any effect on uh effectively calling them in the way that you need them. It kind of just you're kind of just like uh the data will come when it comes from the request, and you you don't really have any control over the order it comes in or how like how fast it comes in, anything like that. It just kind of like, hey, here it runs the hook, it gets your stuff, and then if you have multiple use effects or anything going on, that it could set off a lot of chain reactions. Um yeah, so I I usually just stick the hooks at the top of the file, right?
SPEAKER_01So so that's not quite what I was asking, but I think you also make a good point there. Um that's something definitely that is an issue. What I was really getting at is like let's say uh you're creating some new entity, a new storefront or whatever, right? And so you have a button. It's like create new store. Um, but you need to fetch a bunch of data to like allow the user to select from when they're creating that store. Um, you know, and usually the way it works, you create you click the button and it loads a new page or a new component or whatever, a modal or something to let the user input this data. When is the ideal time to fetch that data? Is it when that component loads, or is it when the user clicks the button?
SPEAKER_00Yeah, it's a good question because there's like the whole concept of like people talk about lazy loading in the data as you need it. Um, but then if there's like if you open that modal and there's like three other uh like I know we have a lot of these autocompletes where we have to search for like a certain item to uh from a list of things, and that like when you open up that modal, the the endpoint to call all of those items, or at least the first like page of them will fire off uh after the modal opens, and there could be like a multiple of those. Um I'm not sure if it's best to wait until that modal is open, or how you would even what what the best way to do that would be.
SPEAKER_01Well, this is what I'm getting at. So I think a lot of people just put the hook in the modal, right, to fetch that data. So when the the modal loads, it's gonna fetch the data. But when the user clicks new store but the new store button, we know we need that data because we know we're gonna open that modal. But if you put the hook in your like new store button or whatever page the new store button's on, it's gonna fetch the data, but that data is not gonna be available to the modal unless you like make sure that that component, that page is still there and it can like pass it via prop, or or if it's not gonna be there, it's gotta pass it up a few levels and then pass it back down, you know, like some sort of global state, right? So like the optimal time to fetch that data from a user experience perspective is as soon as we know we need the data, right?
SPEAKER_00Right. Yeah, and this like so uh there are there are a lot of instances where these data fetching uh hooks are not just they're not on the page, they're not in the modal, but maybe they're like even further further down uh in the tree.
SPEAKER_01Yeah, exactly. And this is what I've seen is usually React programmers, I think, naturally put the hook in the component that uses the data. Um, and this is actually so this is something that I think Facebook has said they're trying to address in React 18. I forget I forget what it was they were they were creating to help address this, but basically that's like the worst time to fetch the data. Then you're making the user wait, unnecessarily. Like you want to have the data there for the user as soon as they are gonna utilize it. But that's really hard to do with just these generic like plain hooks that fetch data, right?
SPEAKER_00Right.
SPEAKER_01Like what yeah, go on.
SPEAKER_00Well, and it makes me wonder too, like what if like what if that what if that data won't even need to be used? Uh like are you calling it unnecessarily um, etc. as well.
SPEAKER_01Yeah, yeah, that's also a great point. Um Yeah, so I think we we've touched on a lot of things here, like um like sort of evolving around this sort of naive, simple implementation, a lot of potential problems. And one of the solutions to these problems is a sort of what I what people will often say is global state, whether that is um uh what we used to use like uh redux and stuff basically for, right? Like you just stuff all your data in there and pull it out, and it sort of magically tries to make sure things don't re-render unless it needs to. Um, but now a lot of people are sort of shying away from Redux. But what I see is them putting it into a sort of global state. So if you have something that fetches data in your component, you might shove it, you know, you might send it back up to like a some parent component way up, and it stores it and then passes it way back down to some other component that actually uses it, which in some ways can actually be better because you might have less fetches and you might, and then you can actually fetch things at the right time. But as we all know, dealing with that type of method of passing global state around is a massive, massive pain. Like you gotta pass callbacks down, you gotta it's it sucks, right?
SPEAKER_00Well, this makes me think of like how the uh use context could be more intelligently used in these situations because especially on endpoints where you know that the the main data that you're trying to get is probably not changing that often. Uh maybe like it changes every day or every like maybe not even then, maybe every week. Um it makes you consider the implications of yeah, why are we why are we calling this then multiple times uh in a day to get ultimately get the same data?
SPEAKER_01Yeah, and I think um you sort of bring up the next version of this, which is using context. So you could stuff all this into a context, which makes it a lot simpler to set and access, right? But what is the downside of that? Um what happens that what what's gonna happen every time you set data? It's gonna re-render basically your entire app.
SPEAKER_00Your entire app, yeah.
SPEAKER_01So so like a lot of this is basically React, you know, pushing us into a direction that leads to a bad solution. Um you know, the simple solution for us is to put hooks into components and not have to like deal with all this other stuff, but that leads to a degraded user experience. If you try to just solve it using React like built-in tools, like passing state around or using context, you run into these other problems. So, you know, a lot of people just stick with their naive hooks and basically a subpar user experience. And another place where this comes in is um what about like an update endpoint? Like let's say you have a store and you're making an update to it. Um a lot of times with these naive hooks, you pass in all the data that you want set on the server, and then the server passes data back to you, but that data that it's passing back to you is exactly the same as what you're sending to the server. You already know what the data is, you're displaying it on the screen. Um but instead you have like this sort of delay. Okay, let the user know we're saving it, you know, wait for it to get a response just to show what we already know. And and this is another case where like you could more intelligently handle this. Um, but you know, you'd have to write some custom logic to be like, okay, it's an update. I'm gonna immediately return this data that got passed in instead of waiting for the data I get back from the server. But if I get data back from the server, I need to like make sure to use it if it's any different than the data I have. So it just adds extra work that it most people don't do, right? I mean, that's that's a another issue with the sort of naive approach. And another big one for me, and this annoys me, I don't know, apparently more than most people, it seems like, or they just don't talk about it, is scroll positions. I want to be able to navigate a React application, the single page React application, like a web, like the internet was intended to be navigated with web pages where I go forward and back, and if I go back, my pages scrolled to wherever I left off. It looks exactly how I left it. How does this work usually in React for you, Austin? Have you experienced this um phenomena working well?
SPEAKER_00Um, no. Usually it's you go back and everything reloads again, and there maybe you even lose like query parameters on a search, or you lose and something doesn't work right and you have to refresh, and so it's just kind of a mess.
SPEAKER_01Yeah, like it I don't know. This this annoys me maybe more than anything about React or any other single page application. If it can't handle basic web functionality, like I don't know. What are you doing? Why do you exist? I feel pretty strongly about this. Sorry. But it's just really frustrating. Like you open, like I'm looking at a list of stuff and I want to look at one thing. So I scroll down, I find the thing I want to look at. I open it up and then I hit back. And now I'm like losing all the state. I gotta scroll down, figure out where I was. Did I already look at this thing? Didn't I? Like, you know, it's really, really annoying. It's a horrible experience. And I don't know why so many people just put up with it. I don't know. You don't you don't seem as bothered by it, Austin.
SPEAKER_00So maybe it's like I think we're just de Yeah, we're just desensitized to the experience at this point, I think.
SPEAKER_01Yeah, it's like, I don't know. For me, it's frustrating because I'm like, we had this awesome thing about the internet and it just went away, you know. Like, like we shouldn't put up with that. So how do you handle how would you handle restoring scroll for what we've talked about with this this hook method where we're fetching data? The old like it would you'd have to like store a scroll position, wait for your data to come back, wait for it to render, then set your scroll position. I mean, that would suck too, right?
SPEAKER_00Yeah, I mean, even though it would it would restore it, it's still like you're sitting there, you're waiting, it's still doing these extra calls, and then it like it clearly like you're maybe the user's even already scrolling looking for something probably exactly. Yeah, it would be clunky.
SPEAKER_01Yeah, it would be horribly clunky. And I think this gets into another like sort of, I guess, pet peeve of mine is paged endpoints. And this has become super common, especially with single-page applications. Um, we you know, fetch multiple pages, and we might show the user the first page, and then they scroll, and whether it's an infinite scroll or whatever it is, we fetch multiple pages. Well, if we go navigate forward and then navigate back, where'd all my pages go? Oh, guess what? The user, as a user, you gotta scroll through them all again, refetch them all again. It's like this is this makes no sense. Like it should be there. I already did that, you know. Um, so I think these are two major pain points that I don't know if people talk about enough, but I think they're a big deal.
SPEAKER_00Yeah, absolutely. I I I never even really the especially the scroll recovery thing. I think I just totally never I I just like totally never even thought about it and just dealt with it, you know. And that's for the paging stuff that that even adds like that adds into the scroll scroll recovery as well. Like how like say you're you're scrolled down like 10 pages, and then it's like how how does your app know to then is it just going to like brrrrrr like fire off 10 paginated requests on the page?
SPEAKER_01I mean, it would even first of all it'd have to know it needs to, so you'd have to store okay, how many pages do I need to fetch when the user goes back? And then what about this? What if they go back two times to and then you know go forward and then like like you gotta like maintain this logic, like okay, I should stop, I should pop it and push it, and like this tracking of my state, and it sounds like a nightmare.
SPEAKER_00Yeah, I mean it sounds like you'd have to create an entire like global state system to like remember everything the user has done. And that just sounds like well, you know it's really funny.
SPEAKER_01That's what browsers do for us. If you're not doing a single page app, they literally do all this, they track where you were, what state was loaded, what you entered. If you I don't know if you guys have noticed, but even for like if you type a bunch of text in like a text box and you click something and you go back, modern browsers will remember what you typed in the text box. Like on a normal web page, but this does not work on a single page application again. Like we totally like the browsers have all this machinery built in to handle it for us, and we're like, yeah, whatever, throw it out the window.
SPEAKER_00Yeah, I mean this this makes me think you could uh use like a local storage hook and like have every everything you've ever all of your uh use states are like stored to a local storage value. It it just sounds like it really, really backwards to get get back to what already exists, you know.
SPEAKER_01Yeah, yep, exactly. So so we have that, and I think you talked about another uh issue, which was like um refresh intervals. Like, let's say you want to fetch data on a like timer, and you can just set up like a set timeout and a use effect, and it's not too hard, right? But what if like um the application fetches the data in the middle of that timer? Do you still want to fetch it, you know, in the remaining like when it's time slot comes up, or do you want to like reset that to you know not be fetching it? Like maybe it fetches immediately after it's just fetched it, you know, if the timer goes off. So like I think there's extra machinery to make the make it good that you'd have to add as well, right?
SPEAKER_00It kind of reminds me of the whole JavaScript in general. It's like um ever like ever since I really started getting into it, it seems like there is so much stuff that has been lost in programming paradigms in the over the years that people are trying to like recreate in JavaScript, and it's like all of these, it's like, oh wow, that's such a cool feature. And it was like, well, actually, like that's been out for a long time, like pre-JavaScript, and we're now just having to recreate it to to get back to what used to work well.
SPEAKER_01Yeah, it's yeah, I think especially as someone that like comes from like especially on the internet. I was using the internet in the late 90s. I know I'm not like OG necessarily, but like I don't know, just to see the evolution of things and exactly like you're saying, like we had this stuff, did we lose it, that we have it, then we lose it.
SPEAKER_00Like it's kind of like Stockholm Syndrome. You you're like, oh wow, like thank you so much for releasing this really cool feature. It's like we're so grateful for you. It's like, but why'd you deprive us of it to begin with?
SPEAKER_01Yeah, yeah. It's it's really fascinating. And I think um, I mean, I think this is a great, you know, transition into talking about the resolution of this. So uh there is a a solution to the problems we've been talking about, but they're not as elegant as the built-in solution to our browsers yet. I think it's a lot better, but uh it is worth pointing out like we're basically recreating stuff that already exists just in a less good way. Um, and you know, maybe at some point, you know, somebody can come up with a React library that does it all correctly or whatever for us. I don't know.
SPEAKER_00But yeah, and then we were back to the one podcast about then you have this dependency where because this thing is like this Swiss Army knife, like you're stuck with it forever because it's like the one thing that can solve all these problems you had, and then you're like you're dependent on that. But I mean that's kind of how any any technological choice is like where think about the library that we're that the dependency that we're stuck with now to make websites like React.
SPEAKER_01Like yeah, so yeah, and I mean I think in my view on that too is like it depends on how like the way in which you're dependent on it. If it's in a more like library type fashion, it doesn't bother me as much, which is obviously why I'm more attracted to React than some other SPA frameworks that are more frameworky. And I I think there's a good reason for that. Um which which is what you were talking about. Uh, but that's a a subject for uh well, probably a previous and a uh upcoming podcast, I'm sure. Um yeah, so let's talk about like uh how do we actually um solve these problems in an elegant-ish, I think a pretty elegant way, actually, a pretty clean, elegant way. And obviously, everyone already knows the solution because they listened to Spar in the podcast. We're talking about query caching libraries. Um and the two I that I'm most familiar with are React Query and SWR, which is a part of the the Next.js toolkit. Um those are the ones I'm most familiar with, but I'm this is like equally probably applicable to any query caching library. Um and so, first of all, what is a query caching library? Um so all it really is, um, if you were uh listening to our our last podcast about databases, actually, it's essentially a database that runs in the browser. It's a key value store or a No SQL database. And the way it works is you say, you basically attach a uh give it a key and then tell it to fetch some data for that key. And it will then fetch the data and use the return value from that or use that data anytime you want to uh anytime you ask for it, so via the key. So the way that it usually works in my experience is that key is like your path or your endpoint path for the data you want. So anywhere in your app, you can be like, hey, give me the data for this endpoint. And the query caching library, if it already has the data, it will just give it to you. If it doesn't have it, it will go fetch it and eventually you'll get it. Um, but then once it has it, it will, depending on how you how long you've configured it, keep that data in its cache for a while. And this allows us to sort of use our nice, simple, naive, hook-based data fetching approach that you know fits well with React without all the downsides of refetching data all the time. And you know, a lot well, we'll we'll talk about you know solving these other problems we brought up. But that's what a query caching library is. Um, did I did I explain that okay, Austin? Does that make sense?
SPEAKER_00Yeah, it sounds like uh the idea of creating this global state that we were talking about to like handle the scroll recovery, etc. Is it's kind of the same concept but for caching your data.
SPEAKER_01Yep, exactly. So it is a global state, but it's um, in my opinion, designed and tailored for this specific task, which makes it better at solving this task than just you know using a used context or or something like that. Um it it somebody actually put effort into designing it well, um, theoretically, assuming you you choose a good query caching library. Um, and that's really the big advantage is somebody put in a lot of this extra work to handle the fetching logic and the you know, all this other stuff, scroll recovery, that kind of stuff. They put the extra work into it. And another thing that they usually put extra work into is the dependencies. So that situation we were talking about, like, in fact, I don't even know if we really talked about it much, but what about the case where when I update some data, I need to refetch a bunch of other data that may have changed because of my update. You know, doing that in plain vanilla hooks, you can do it, but it's in my experience, like really tedious. You have to be like, okay, call this hook and this hook and this hook, and you have to remember to call the right ones every time. Like anytime you this data changes, you have to remember to do it this way. And it it gets tedious, and I find a lot of people make mistakes, you know. Um but yeah, so so that usually these libraries are are written to handle this, they're tested for it, um, and and they're good at these types of situations.
SPEAKER_00Yeah, I never even realized uh how how nice they are until we made the switch over to React Query on one of our main projects and began implementing it, and it was like, oh wow, all these little like little things I didn't even realize were were messing up, just like resolved of themselves. Uh it's been really a really worthwhile experience. And even though there the there is a bit of learning curve when you use any library or any new paradigm, I think that um this really ultimately pays off. And well, I I know this might be a little bit out of order, but like we'll get to like when when you should implement uh this in in the app later. Sorry about that. My my brain, you know, I get I get thinking.
SPEAKER_01Yeah. Well, I wanted to follow up and say that I actually was really skeptical. Um in my programming experience, adding caching is like something people often do, but they do it too early and they do it wrong. Um and if you don't understand your caching, if you don't do it right, it actually leads to way more problems than it solves. And so I actually didn't pick this up nearest, like I should have picked it up way sooner than I did. It was my own like sort of bias against caching because of bad past experiences. So I'm actually with you, Austin, and like I didn't realize like how many problems it actually solved without creating a bunch of new problems, you know.
SPEAKER_00Well, I mean, you're you're right though, about like when when do you implement it? And I think uh quote bottom-up approach would to be figure out like wait until you are running into issues, uh, or if you like already intimately know how to like do the cat like how the caching will pan out and affect your application as you're building it. It's definitely uh you have to have some experience with it before you before you implement it from the from the beginning, you know.
SPEAKER_01Yeah, I think um I I think we will get into this. I want to define things a little bit better for people if they're not familiar with it first. Um and and so that would be like um just yeah, so a query caching library from a high level, the way you would use it usually is you're like, hey, query caching library. Um here's my basically your path, and here's the like a function that fetches the data. Um, and then it will return a bunch of different things, like the data, the whether or not it's loading, whether or not it's fetching, if there's an error, you know. Um, and the thing that I want to point out is in when you're using this interface is they're usually all what's called stale while revalidate, which is different than I think how a lot of people um will naturally write their programs. That basically means let's say the cache doesn't have the data yet, it needs to fetch it, it's gonna return null as that data until it gets the data, in which point it will return the actual data. And so your application needs to be written and designed to handle your data values being null for a while. Um, but yeah, so that's the basic way it works. It's pretty simple. You just say, hey, fetch this data, this is the key I want to use, which is usually the path, and you know, you can use that data as it comes back. And if it already has the data, it'll just return it immediately. But the stay a while revalidate part also applies here. If you are getting a cached value, usually these libraries will immediately fire off a request to get fresh data. So you're making sure that your user experience is really immediate. Like the user gets the data right away, they can use it, but they're also not getting stale data that doesn't get refreshed. Like the library should immediately be going, okay, I'm gonna fetch all the new data that we need for this screen or whatever. So the user sees probably what they need right away and certainly what they need very quickly. Um, so it's a little bit of a different paradigm, but that that's the way they usually work. And it takes a little getting used to, but I find that it's actually pretty natural to the way React works. Like you just if value is null, show a loading screen or something. If it's not, show your data. You know, there's a lot of different ways you can handle it, you know, show a skeleton or whatever. Um, but yeah, so that's the the basics of the query caching library. And then usually you can specify dependencies. So you can say, if this thing gets uh updated, if an update request gets sent to this endpoint, I want you to refetch all these other endpoints. And maybe somewhere else in your app, for one of those endpoints, you're like, hey, if this endpoint gets fetched, I want you to fetch these other endpoints. And so you can basically build this chain of dependent data without having to like manually piece it together by calling hooks. Um, I find it to be a much more declarative and easier to work with um system than you know, maybe the more naive approach. I don't know if you agree with that, Austin, if you had the same experience.
SPEAKER_00Yeah, I I don't have a whole lot of experience with this. Uh the all all of this caching is quite new to me. So this this is a really informative episode for me. I I appreciate your insights and what what you're able to teach us here today.
SPEAKER_01Oh, well, thank you.
SPEAKER_00Of course.
SPEAKER_01Um, yeah, so I think that's like a good overview of of how this system works. Um so I do want to say before we move on, um, there are some downsides like there is to any caching. Um like I just mentioned, you might be showing stale data sometimes. And usually these caching libraries will let you um actually specify whether or not that's okay. Like um, you should be able to control that, but you know, there is that risk if you're not paying attention, you could show stale data when you didn't mean to. Um, and caching is always a compli like it adds complications. So you have to understand how it's working, or you will get weird results you don't understand.
SPEAKER_00Yeah, and these like uh when you're showing stale data, I mean that that's when you have to know like when when would it be okay to show stale data like that? Um, but like there's a lot of cases where that that just is not going to be okay for the users of your application where maybe they're making some like really important decisions based off of what is shown, and you're showing them old data, and it's like yikes. So the implications definitely should not be taken lightly.
SPEAKER_01Yeah, yep, definitely. And and there's definitely places where it's not appropriate, it won't make your application better. Like I think of so the the one that I bring up a lot that Austin's familiar with is this farming game that I wrote. It's like a you know interactive multiplayer online um sort of like recreation of a board game that is all everything happens in real time. I click a button, everybody should see the response right away. There are maybe a few cases where caching would help, but basically that's just not appropriate. So, like what Austin was saying earlier, like it doesn't necessarily mean, even though I said it in the beginning, you don't always need to have a query caching library. But for most, I would say 98% of React applications that are like crud-based, type, you know, create removed update and that that kind of thing, you almost certainly would benefit from one. Um, but it is important to know it's not all the time.
SPEAKER_00So more more application types where it's like you're dealing with like live data that's coming in and you need to be like really like when there's a lot of things happening really fast, it it doesn't really pay off to do caching as much as if you have uh like maybe slower running business processes in your application. Is that what you're saying?
SPEAKER_01Yeah, and like it depends on the architecture. I think your architecture, if it works for query caching libraries, it will just kind of work naturally. Whereas like in the farming game, the way my architecture is, is I have two WebSock open to the server. One receives messages from the server and one sends messages to the server. And the messages I receive from the server are the current game state, basically. So anytime any player does anything, the server says, here's the updated game state. And what I send to the server is just like, hey, I clicked this button or whatever, you know. But you're if you were to add a caching library, you'd be like, Well, I don't even know where to add it. There's no like I don't nothing to do, you know. So I feel like it's we don't have to think too hard. About it, it'll be kind of clear. Like, if you're like, hey, I don't know how to add it, it doesn't just doesn't make sense, then you probably don't need it. But if you're like, oh, it would be easy to slot it in here where I'm fetching this list of users or something, then yeah, that makes sense.
SPEAKER_00Yeah, yeah. So it's like you don't then necessarily have to cache all the things, it should be used as like there's certain places and times to use it and others not.
SPEAKER_01Yeah, absolutely. Even in an application where it fits perfectly, there's gonna be probably things that there's no like like what about this? Um, like you log in the user via um uh what's it called? The the thing where uh it's not via cookie, but um uh the JSON web tokens. Yeah, yeah, yeah. So so do you want to cache that? Like, you know, like like what that doesn't like cache a login? I mean, there's things where like it doesn't make sense no matter the application. So you gotta use your brain a little bit.
SPEAKER_00Yeah, that could lead to some uh unintended side effects for sure.
SPEAKER_01Yeah, exactly. It is it is a caching, you can't just apply it blanket everywhere, which is what a lot of people want to do because that seems easy and straightforward, but you can't. So I I don't want to give anybody the idea that this is like a no-brainer thing you don't have to think about. You do still need to think about it. Um, yeah, so I think we've talked about, you know, sort of the background and the downsides. Let's get into solving some things, right? This is I know this is Austin's favorite part always. So let's get into it. Um, the first thing I want to say is like um before we even talk about like some of this more technical stuff, um, you don't have to get too overwhelmed if you don't understand because React Query, at least, and I don't know about like I haven't found this to be true with SWR and some of the other ones, but React Query, if you start with that to at least learn, it has really good dev tools that'll actually like be on the page and it'll show you what's in the cache, what is what's being fetched, what needs to be revalidated, like all of this stuff. I found that to be a tremendous help when learning it. So if you're new to it, or even if you don't understand stuff, I would recommend checking that out. Um, I don't know, do you ever use it, Austin?
SPEAKER_00Uh the DevTools, the fur RQ. I I have actually, I don't think I've ever clicked the icon down in the corner. Uh I see it every like every time I load up the application, and I just was like, oh, it's a cute little icon. I honestly didn't even know that you could view all of that stuff. That that's really cool.
SPEAKER_01I'm glad we're doing this. Yeah, it's awesome. So it it shows you like um all it'll show you like the key, it'll show you the data associated with it currently, and and all the other stuff around fetching it, the dependencies, all of this. So you could just see it in one nice place. And and if you're like, hey, why is this not updating? Why is this not showing? What why does it keep refetching? Like, that's the place to go. You just pop that open and it's usually really clear. So I highly recommend that for anyone that is new or anyone that's not new but didn't know about it.
SPEAKER_00And so the it will only it will only show the results of endpoints that are attached to React Query, or will it also provide information on other res uh requests that are not within React Query?
SPEAKER_01It's just for things within React Query.
SPEAKER_00Okay.
SPEAKER_01Yeah. Yeah, so you can have that popped open. Usually I have that popped open and the network tab, you know, in your dev tools. And you can you'll you'll be able to see it. And it's really nice too, because like um it it it will show you like a great example. Um React Query and SWR have um what's called um I forget what it's called actually, but when you um lose focus on the browser and then you click the browser, it will refetch it. Yeah, so so as you bring it back into focus, all of the data that is currently like needed for the the page that's being shown will be refetched in the background, right? Um so a great way to debug things where you're not understanding what's being fetched, especially with dependencies and stuff, is I'll pop open those dev tools, pop open my networking tab, and then I'll defocus the window and then I'll focus it again because that triggers everything to get refetched, you know. And you can just look right in the React query, like, okay, this is going, this is going, this is going, this is what it returned. And a lot of times your problem will be you're using a different key than you thought you were, which means a different path slightly, like, oh, it had a slash on the end, or oh, the query parameter was different. This is uh often one that will happen is um you can so actually the the key for for these libraries is actually an array. And usually the way I use it is um the first thing in the array will be the path. So like slash resource, you know, and then the next thing in the array would be any like query parameters or whatever, and it would be as a JSON object, and these libraries are smart enough to dedupe and like um compare these, like do a deep comparison of these like things in your key. So you can pass in your query params, and even if the order changes or whatever, it doesn't matter, you know. So what often would happen is I'd be like, Oh, why is this refetching or why isn't it refetching? And it turns out it's because like the query params were different or whatever, and you can easily see that in the the racked query dev tools.
SPEAKER_00That's pretty slick.
SPEAKER_01Yeah, yeah, it's pretty awesome. I think um that's a good tie-in to talking about yeah, how to specifically implement this, and it all comes down to your keys mostly, and so I've mostly detailed this. Um, I would highly recommend the first thing in the array that you use as a key will be the path um to whatever resource you're fetching, and then the second thing be any query parameters stored as a JSON object. Now you I have done slightly fancier things like that with other uh or with like um other things in the array, um, but I I don't know if they're really it gets it gets complicated because like you need to make sure you're using it the same everywhere. And so that's an important, I guess, caveat. But if you use those two things the same everywhere, I think you'll be in pretty good shape. It'll get you pretty far.
SPEAKER_00Do you have any examples of like a third thing that would be in the array that you've you've messed around with before? And what was there any reason that it was necessary?
SPEAKER_01I mean, so the reason why you would have it is if something about your request fetching is gonna be different. Um there and if you're using a regular like rest interface, um there should never be anything different because it's all based on your method, your path, and the query params, right? Um so the only time I've ever used it, I'm trying to remember. I think I think the only time I ever used it with a rest interface was when I was debugging things. I'd be like, hey, I'm gonna give it a third thing and see how that changes things. Like otherwise, for rest interfaces, I honestly don't know what you would do with it. Um there there might be a case for if you're like trying to associate like dependencies or something, but I would think you're better off using the built-in system if that is available.
SPEAKER_00Yeah, which is a cool topic uh within within React Query. The dependencies allows you to really fine-tune what data does get updated when you need it, uh, based on like we were talking about earlier with optimistic updates, how how messy like uh previous implementation of that used to be, where we would like we had all these functions to like find the find the object within the array of objects and then uh like pass it in there so it would update and then when the server request came through we would clear the entire list to make sure we had it, so it was like it was almost pointless to really do it, and it just caused like a double re-render uh at minimum uh on all of these endpoints because all this logic was also stuck into like a hook that was used everywhere.
SPEAKER_01Um yeah, you're totally right. That's what most people do though, because they're like, Well, I don't I don't know what should be refetched when, so I'm gonna take the easy route and just re-clear everything, refetch everything anytime anything changes, you know. Right, it's just the way to go. So the ability to specify like a fine-grained dependency list is amazing. It makes a big difference. And I did also want to touch on something you mentioned too, which was optimistic updates. So I don't know about other ones off the top of my head, but React Query does not by default do optimistic updates. Um, I think mostly because the way in which you do them depends on like the back-end responses you're getting. Um, but it's easy to set up. So in React Query, you can set up basically hooks. Uh hooks is the wrong term in this case, because we're talking about React. You could you could set up like uh sort of callbacks, I guess would be the technical term, on uh when when you're setting up like an endpoint in React query. So you can say, um, when we get data back from the server, run this code, or before we send data to the server, run this code, or whatever. So so it gives you the that um ability to hook into its mechanism. And what you can do, um, in fact, they give you examples right on their documentation for how to set up optimistic updates so that when you get um when you're sending before you even send data to the server, you set that new data as the data in the cache. So anything that's using it will immediately get access to the new data. And then as it gets data back from the server, it updates the cache if the data has changed. Um it's really quite brilliant. And that brings me to something we totally didn't talk about yet in this part, which is um these libraries do a huge service to you in terms of re-rendering because they're not fetching new data all the time and returning new data that causes re-renders. Like you say, hey, give me a value from this cache, it returns the value it returned last time. No re-render. It's brilliant.
SPEAKER_00Um, and so it it hooks into React's key-based uh system as well to ensure that like the the keys within the cache match uh like directly to that instead of because like you mentioned um before when you like will refetch, you'll fetch some data even though it hasn't changed, the the React application believes that it has changed because it it can't like identify that nothing actually changed. Um is this like using uh how how does React query leverage its keys to like intelligently show React that it should not actually be re-rendering?
SPEAKER_01So uh the only um the the way that it works in React is React only looks to see if the object or the data it's using is in a different memory location. Like it is it it's not like React doesn't do a comparison. So if you want to ensure you don't get a re-render, you have to do a deep comparison on the result. And if you do a deep comparison and you find out, hey, my data didn't change, you just use the value that's already in the cache, and then it'll all just magically work. Um and I thought uh yeah, now I'm like getting worried. I'm like giving people bad information. I thought um React query in such libraries did do a deep comparison, but I am not now. I'm like wondering, is that not true? I thought it did, but either way, you're still gonna be better off because you're using the data from the cache a lot of the times instead of the data from the response, so you won't get as many um re-renders. Uh, but I did think they had the like comparisons where it didn't return new data unless the data changed. I will have to look that up.
SPEAKER_00And so, but what you are saying is that it is mainly focused around React will because React Query uses it is cached and it is in a memory location, the um React will know because the way React Query is passing in the cache data, it's like, oh, it's literally in the same place, it's exactly the same, right? So that's cool.
SPEAKER_01So you won't get a re-render, yeah. It'll absolutely, in my experience, it dramatically reduces the amount of re-renders you get from data-related stuff. Um, either it's not gonna eliminate them because sometimes your data is gonna change, but like at least it's not gonna be doing it all the time for no point.
SPEAKER_00Right on.
SPEAKER_01Um, so yeah, it is a big difference. Um and yeah, so I think we can talk about oh the the fun thing I was ranting about earlier. So that scenario where I was like, oh, you click the button to create a new thing and it brings up a modal, when's the ideal time to fetch this data? It's as soon as we know we need it, which is when they click the button. But using React normally without this caching mechanism, that's just not a thing. Like that would be way too much overhead, nobody's gonna do it, right? Well, I don't know if you notice this, Austin, but actually I use this technique I'm about to talk about, and that is with React query, all you have to do is use the hook, and it will fetch start fetching that data. And since, like, let's say I click start a new, you know, create a new store. That button, when when the user clicks it, before I show the modal or whatever, I just include all those hooks, you know. So it just starts fetching all that data. Then it shows the modal, and that page also includes all those hooks. But they, you know, are like, hey, React Query, you know, get this data for me. React query is like, I got you, bro, no problem. I'm already fetching it, you know, and so it doesn't do a second refetch. It's not like doing two refetches in a row, but it just gets as soon as the data comes back from that first use of the hook, it's gonna be available in this other place as well. And so you end up getting your data a lot faster, and it's super easy to use.
SPEAKER_00That's pretty amazing, actually. I mean, it's these it's little things like this where we we've talked about in the past how like is your React app killing the planet, uh, but because of these like excessive re-renders and everything, and just like these. I mean, uh the this library, the the guy who made it, um it's part of the Tan stack, uh uh Tanner Lindsley uh made React query, and it's it's really it's really great that people have taken the time to uh go out of their way to create these solutions for themselves and then share them with the community. And uh they they really do make make a difference. And sometimes when we get on these shows and but someone will like ask me, what what do you do your podcast about like I I don't even I don't even know if I'm want to explain it to you because it just you just nod your head and but uh the these little things like this when you implement them and you see like how much snappier your application becomes and it just feels a lot more like you can take a like a crud app and make it feel a lot more real-time and like interactive than than it was before just simply by beginning to use React query.
SPEAKER_01Yeah, and that's a great like tie-in to the example I was gonna give. So in a a project that you know both you and I work on, there's a case where like um the user eventually needs to print something. Like, um, and to print something, they need the list of printers so they can choose which printer to send it to, right? And the printers aren't changing, you know, people aren't adding and removing printers all the time, right? So it's a perfect thing to cache. So the naive implementation is we show the screen with the printers, uh, we fetch the printer data, and the user sees the loading indicator until the printers are there, right? I mean, that's the normal way to do it. Well, in the screen before it, I'm like, oh, use printers hook. And I don't do anything with the data, I just use the hook. And then my printers have already been fetched and they're just there and ready to go on the next page. I don't you don't have to do anything special. It just it works better, it's a better user experience, and it is a great developer experience, which I think is one of the best parts.
SPEAKER_00So, can you give an example of how how you're uh using this hook? So, like on the page, like before you know you need the printers, you are calling the hook to just pre-fetch them on that page, and then within the page that you actually do need them, you're you you're calling the hook again.
SPEAKER_01Exactly. I just call it the same way both times, and it's just pre-fetching. There's so it actually does, there's there's other ways to pre-fetch that the libraries provide, but that way works perfectly fine. And yeah, I love it. I I actually use it quite a bit now. It's just like I don't know, it's such a nice experience where before it was like, wait, and now you're just like boom, boom, boom, boom, click buttons and and go on, like you know.
SPEAKER_00And so when you're um like say they're on the first page, there is the printers that you need. Uh well on the second page you'll do something like const squiggly brackets printers equals use printers. Yeah. Right. And then on the prior page, though, would you destructure the printers out of the hook, or would you just uh I don't do anything just use the hook, you just execute use printers as a function. Okay.
SPEAKER_01Yep. Yeah, that's it. Um using it for the side effect, is what I'm using it for.
SPEAKER_00I got you. That's cool.
SPEAKER_01Yeah. Um it's yeah, and and the thing too is again, this is caching. So you don't there is a cost. So maybe the user never makes it to the next screen. Well, now I've just fetched the printers when I didn't need to. So you don't necessarily want to just pre-fetch everything all the time. But like it the way I look at it is is there a really good chance they're gonna use this data? And unless fetching this data is a super costly thing, then I'm gonna fetch it. Like it's not that big of a deal, and it leads to such a better user experience.
SPEAKER_00Have you ever um have you used this um paradigm to ever fetch anything at a much higher level of the application, or would you recommend not getting too far ahead of yourself with these kind of uh these kind of pre-fetching by just do it like on the page before primarily?
SPEAKER_01Um well I I just asked that same question. How likely like are is the user to eventually st see this data before it gets stale? Um if it's very likely, and if it's not an expensive server process or something, then yeah, I'm gonna do it as soon as I can, you know? Like there's just no reason not to, in my view.
SPEAKER_00Yeah, and especially for stuff that is definitely not changing for a while. Like right when you open up the app, you might as well. Hey, usually when people open this app, they they use the printer, it's part of their daily workflow, might as well just do it at the highest level.
SPEAKER_01Yep, and you know, maybe later on, you know, they don't use it for a while and you refetch it later. Who cares? Like it's not really a big cost in that regard. Right. So it all it uh all comes down to that cost analysis. And I don't know, it it I just love it because it's so easy to take advantage of. Like you don't have to do anything special, you just use it, it just works.
SPEAKER_00Right, especially like if it's going to be happening, if the endpoint's going to be fetched anyway, if there's a high likelihood it's gonna be fetched anyways, like there really isn't any downside, uh unless the data is changing all the time, right? Right. Um so that's that's really I never even thought about really doing that, and now I'm gonna look uh look back at uh everything that we're implementing and pre-fetch all the things.
SPEAKER_01Yeah, yeah. Um yeah, definitely. That that leads us to my other favorite topic: scroll recovery. Guess what? It just works if you use this. So the the thing is these libraries actually usually include scroll recovery built in. So if you're not like using a div, if you're not like scrolling a div within the page, if you're just like scrolling the page, it will actually just work. You don't have to do anything. If you're doing your own like uh sub pages or something, um you'll have to rec like store the scroll position and recover it potentially yourself, but because your paged endpoints will already be in the cache, you know, and all your data will be in the cache, you can hit the back button and your page will just render like the user last saw it, and you can restore the scroll position immediately, just like the internet was intended to be.
SPEAKER_00And uh just a note on this though, React uh query does invalidate um after five minutes, I believe, by default.
SPEAKER_01Um what what does invalidate mean?
SPEAKER_00Or I mean it it doesn't it doesn't it it will like refresh, it will like make sure that the data it'll re refetch it every five minutes to make sure that it is accurate, right?
SPEAKER_01Yeah, correct. So that doesn't mean it's purging the data and has to refetch it. It just means it's gonna get you new data after five minutes. And you can customize that per query and in general.
SPEAKER_00Yeah, which has been something where which has been really useful recently in uh debugging excessive uh server calls that were being made from an application we've created is uh in making sure that we like the the cool thing about the like instead of having some like refetch interval where you have to apply all the the query parameters, etc. And like in like we said, there may even be uh duplication of uh refetching where it'll it'll refetch a second before because of a user action, then it will execute again. Um customizing the refetch interval uh on a React query endpoint's really easy and it it takes into account um the key the key path, including that the the endpoint and the parameters, it will intelligently fetch all of that uh on the interval that you desire to specify.
SPEAKER_01Yeah, and and you used it recently even for um customizing it uh right? So like yeah, you want to tell us about that?
SPEAKER_00Yeah, some users we some users were needing to have data on certain pages refresh refetch at certain intervals just to make sure that they could track statuses of things as they changed uh from user input. And uh prior to that, there was they were using a Chrome extension to reef refresh the entire page. And sometimes um the query parameters wouldn't be uh properly done. There were definitely no scroll restoration if they had a long list. Uh there a lot of little things that just it was ugly, and all all we did was we created a little a little drop-down where it's like, hey, how like in milliseconds. Well, I I didn't let them put in a custom input. It was only a few options they could choose, but I I stored that refetch interval to local storage, and now uh different users can refetch certain views as they need within the application and really easily, and that includes all the caching out the box, all the scroll restoration, etc. And um it's made the experience a lot cleaner with just like a really simple like you literally just pass in refetch interval into the query, and it's you have it.
SPEAKER_01And and if I remember right, we discussed implementing this before we had added React Query, right? Do you remember what the conclusion of that was?
SPEAKER_00Uh no, I don't because I just like my brain kind of blurred and went blank when we were trying to discuss how we were going to do this, and I was like, yeah, I'm pretty sure like I'm never gonna get around to that one.
SPEAKER_01Yeah, that my I don't know that my my brain blanked, but it went I'm not doing that because that's like it was gonna be a ton more work, like refactoring things, and so I was like, I'd rather just do React query, then it's easy.
SPEAKER_00Right, yeah, and that's where like we we were bound, like in that one project, we created this like mega hook that fetches resources or fetches lists of resources, and to go in and be able to implement a feature like that, you couldn't just do it on the one uh endpoint use case, it like it would ripple out and affect everything. It was just like this is like no.
SPEAKER_02Yeah.
SPEAKER_00And then React Query, sure, there's been a bit of a learning curve with it, but it's really not like if you start with like a simple use case and like figure out how it works, it really isn't uh too too difficult to grasp. It's just like anything. You have to learn a little bit of paradigms and like a way something works, and then the the benefits like far outweigh the time you have to put in to learn it.
SPEAKER_01Yeah, absolutely. Yeah, uh that that's a great use of React query again, in my opinion. Um, and I think we've talked about a lot of sort of solutions. Um I'm I'm curious now, what about you know, actually implementing this in your application? Like um, you know, would would this be a hook that encapsulates the the React query stuff? Do you just uh or or SWR or whatever it is, or do you call those directly? Or um I don't know if you have any opinions on this, Austin, but I knew you wanted me to talk about it, so I'm gonna ask you first.
SPEAKER_00Well, like in that uh that project that we're currently on, we have like an entire mega hook that handles all sorts of stuff for either a resource or a resource list for with React query, and we we've gone that route, and it's been really it's been really in ways like nice to have that uh out the box. But I mean if you're uh if you're going to be implementing it yourself, we we often talk about this quote bottom-up approach of like implementing what you need as you need it and looking for repetition as it develops over time before making uh attempts to abstract, right?
SPEAKER_01Absolutely, yeah. That was what I was gonna say. So um, yeah, I think um the best way is absolutely like Austin said, just write it the way you would with React query or whatever, and then over time look for patterns. And I think you will find patterns probably things that are duplicated that would make sense to create some sort of generic hook that creates your React Query endpoints or whatever. Um but yeah, definitely, you know, like we always say, do it bottom up and and you know, don't be afraid to end up going that route. But definitely I would recommend against starting there. Um, I think part of the problems that Austin and I have seen is in projects where they try to create a generic resource hook, like Austin was saying, and it doesn't always map to everything well, like the everything so you end up what ends up happening is things get shoved into it like in ways that you know don't always make sense. Um so definitely, yeah, avoiding that is is um always beneficial, as we say.
SPEAKER_00Right, and uh not like not every use case that you have will be able to map to these like abstractions that you create, and then these abstractions can even lead to like big problems down the line. Like we're having some issues uh server side with uh um abstractions for filtering uh that have been made and sorting and stuff, and then there's all these different use cases that are occurring now as the application is developed over almost two years, and it's like wow, this like this is all hooked into that abstraction, and it's like not like some major rewriting is going to be needed to needed to be done. And like you get to these really painful points that you've created yourself, and it's like like how how are we really going to move forward? So it's really important not to get too far ahead of yourself and let things kind of develop naturally over time, and then get to a point where you're like, you know, I've I've written this same uh like implementation that hooks into React Query now at least like four to five times. I think it's like time that I could extend uh that that use query hook into its own hook and figure out a way to to standardize my processes.
SPEAKER_01Yeah, I I uh 100% agree. Um and I think you know, uh I think we're sort of winding down with you know what uh what I had imagined. Was there anything else you wanted to uh leave people with, Austin, in terms of React query or anything?
SPEAKER_00Or uh just don't be afraid to learn it. Uh for a while I was like I was like, yeah, like it sounds like a nice to have, and there's some pretty cool, uh, pretty cool benefits that come with it. And like you may not realize how uh clunky the experience in your application is where you do need caching. And I would say it is worth the it's not that difficult that you're going to spend a whole lot of time uh to really dig into it and understand how to use it uh out the box uh to get the benefits that that come with it pretty quickly.
SPEAKER_01Yeah, that was exactly what I was thinking again. Yeah, don't be afraid to learn it. And and what I was gonna say is like just to try it out, create a little basic project and and see what you think and get a feel for it. Um and I mean I gotta, and I guess to leave everyone with something too, in this sense, is like like I said earlier, I kind of was really hesitant to get into this, you know, like in terms of learning it, like Austin said, but also like in general, using the caching, but like from a from a purist sense, it's not really like we need the caching for caching's sake, but the way React is designed in and the architecture that falls from it, I think it basically requires it in a lot of cases. Like, if you want to have a good experience. So, while in a lot of other non-React world cases, I basically never use caching because it's just not necessary. I I really believe for React applications, it is the most natural, most like bug-free way of implementing a lot of common features that we need and that make for a good experience. So if you haven't given it a try yet, definitely do that and let us know, you know, what you think about it. Um, yeah, so I think that that is about wrapping up our show today. You know, we we talked about how you must use query caching libraries and then talked about how that's not quite true, but mostly true. And we got into some examples of how we see people do things without query caching libraries, um, and a lot of the pain points, you know. I ranted about scrolling and paging and you know, wonderful things like that. Um, and we also got to look at solutions. Uh, you know, how can we actually solve these problems using React Query, SWR, and other query caching libraries, what those libraries actually are, you know, key value stores basically that handle data fetching. And um, we also, you know, then took a look at how do you implement this in your own application, which I think, you know, was a lot of fun. Um, and and I had a lot of fun doing this today. I I can tell Austin did by by the the big smile on his face. And and we were very happy to have you all join us today. Um, yeah, did you uh wanna uh say anything to to people for the upcoming week or uh you know anything going on, Austin? Or is it time to sign off?
SPEAKER_00I think we're we're all good here. Uh just like Thomas said, don't be afraid to dip your toes in the the caching world and give it a try.
SPEAKER_01Yeah, all right. Sounds good. And like always, you could send us feedback or questions directly. Uh email us at the react show at owlcreek.studio. Uh thanks for joining us once again. We hope you have a great rest of your day. Bye. Bye.