SPEAKER_01

And welcome to the React Show. We got an awesome show for you guys today. Maybe a little bit contentious, but I think it'll turn out well. I'm sure it'll turn out well, actually. I'm Thomas Hinz with Owl Creek. We're a high performance React Consultancy with my co-host Austin. And yeah, we got a great show for you all today. Once again, as we mentioned recently, if you have feedback or questions, send us an email to the React Show at owlcreek.studio. Yeah, so our show today is about use effect and how you're probably using it wrong, even if you don't know it. Yeah, we're gonna we're gonna dive into that. Um but first um I I was thinking um we we'd get Austin's take on it. So so what do you think about use effect, Austin?

SPEAKER_00

Well, there are a few things. Um I definitely after we talked about uh use effect preparing the notes and over the last weeks and the the contract work we do together, I definitely am using it wrong in a lot of cases, but it's been nice learning. Um but even with using it wrong, perhaps I really do like the use effect uh pattern because one, I think the functional approach to the React components versus the class basic approach is a lot cleaner, like less syntax, of course. Um, and I feel like the ability to have uh perhaps multiple use effects in a file based on certain uh within a component based on certain like effects that you need to run is cool. It helps you encapsulate uh things that you need to run, uh effects that you need to run. Um the one thing that I find is a little not so great is with the prior like component will mount or like did update, you had access to like the previous state and the previous props by default. And I'm pretty sure you can't access those in the use effect. You have to perhaps create another hook where you store the previous value in a ref or something like that, um, which can be a little a little strange when you're trying to um use that hook with use effect because you can't use hooks within like functions and stuff. So I feel like it's it's made some pretty cool things, and it's also kind of made me scratch my head and wonder how to do things that used to be simple but now seem as simple.

SPEAKER_01

Uh yeah, actually you make a lot of really good points, and I I would agree actually on pretty much all of it. Um well I I I did uh I think um put a bullet point at the end of our show notes here for for us to talk even more about how we feel about it, and so I'll probably echo some of what you said um later on down there, um along with some other things. Um but yeah, no, I I I definitely agree. Um it it's it's definitely has its merits. Um so I'm curious, uh before we talked about use effect, um, did you did you have any idea that you might be using it wrong? Or or did it just all seem natural and intuitive to you?

SPEAKER_00

Uh well I think we talked a little about this uh earlier this week where when hooks came out, um I just I didn't fully read through all of the documentation on use effect because it was more like a on an as needed basis. I was like, oh hey, I need to replicate this componented mount, and um, how do I do that with hooks? And so I like skimmed through things a little and just like it wasn't fully clear to me uh at first. I think the thing that um was initially confusing was the dependency array uh stuff and like how to make that uh work at first. And I noticed that in a few of the applications I am partaking in, the development of that seems to be one of the biggest sources of the warnings that I get in the applications is the dependency array uh issue in general, like what to put in there and why to put it in there, uh, which I think uh I'm just now getting like real clarification on and how to use that properly. But otherwise, it it wasn't too bumpy to begin implementing it. Um perhaps like the the how to like write asynchronous functions within a use effect was interesting. I learned some cool new syntax on like auto-invoking functions that you can do in there, etc. But other than that, I feel like it was pretty easy to adopt.

SPEAKER_01

Yeah, um that that's actually really interesting. Um, I think a lot of what we're gonna talk about today is that dependency array, um, because that is definitely the primary source for where I say people are doing it wrong. Um, and it seems to generally come from the same approach that you took, I think, which was just you know looking at at the docs to some degree, but maybe not like fully understanding the technical implications. And at the same time, also being like, hey, I was able to do this with classes, how do I do it with with hooks? Um, which I think is a perfectly natural response, a natural thing to think. Um, and and beyond that, I think some of those things that we did with classes are something that needs to be done still with function-based components, and it's not as straightforward. Um, and so I think all of that makes a lot of sense. Um and I think it's a great segue into the the next topic here uh about use effect, which is actually talking about how use effect works. Um, and so I think it's important to start there um so that we can like actually discuss how to use it correctly. Uh it's harder to do if if we don't have a good um shared understanding of how it works. Uh so so I would say like where we start is we have a render in React, right? Um and and in React functional components, um you're not allowed to do things that um cause side effects inside of a render. That's just forbidden, right? Like you want to make an API call, you can't just make an API call and await the response inside your render code and use it. Um you'll you'll get in big trouble. React will spank you hard, you know. Um so so they did provide a way to let you do those things, but it's this sort of indirect thing called use effect, right? Um and the way that works is React starts executing the code in your component, and it gets to a call to use effect. And it is a call, it's a function call. And the first argument to use effect is uh a function. And so all React does is say, oh, okay, I I'm gonna have to run this effect later. And so it stores that function, um, maps it maybe to this render in some way, whatever, and it just stores a list of effects as it goes on. Um so maybe you have three different use effect calls, it stores those three functions that you pass to use effect. Um, but then it just completes doing the render. Like it just basically it doesn't run your code. It does the complete render, it doesn't run any of your use effect code, right? Now, once it's done doing the render, React is like, okay, now I'll go execute the code inside of the use effect. And it goes through that list and just executes those functions that you told it to execute. So, and that's really all it is. It's pretty simple. Um, it just delays you just delay that execution until after the render is done. Um, and that um, you know, it does actually have some benefits because like it forces you to decouple you know potentially long-running operations and and and such from the actual render of your component, which it I don't know if you've done much like game programming or anything, but you know, it's sort of like your your inner you know loop in in a game where you're just trying to render the next frame and you're not trying to necessarily do long-running calculations. Um, that's kind of like I think what they're going for with this system is your render should just be quick and straightforward, and React should be able to do a render whenever it wants, and and things should just work. And um, you know, it should be able to call this render a hundred times in a row if it needs to, and not have to worry about kicking off a hundred API calls, which is obviously the other part of use effect, you know, separating effects from rendering. And so there are reasons for it. Um, but but now we can see like, okay, it's pretty simple. All we do is run this code later. Um, and we'll talk about like the dependency array later, but by default, that means your use effect code always gets run once for every render. Um, and you know, I I don't know, to me that makes sense. Um I I see once we get the dependency array into this, it it confuses people. But does that make sense to you so far, Austin?

SPEAKER_00

Yeah, with so without the dependency array, it will the use effect will execute on every render.

SPEAKER_01

Yes. Yep. So that's just the way it works. Um and and so when I look at that, um, I'm still ignoring the dependency array, but let's say I I look at at just use effect and I'm like, okay, I only want my API call to happen once, or I only want my API call. How about a better case? Let's say I only want this API call to be triggered when a prop's value changes, like something else is passing in a different um item for my select you know component or something, and I want to trigger a new API call. Well, the natural way to implement that would just be an if statement. If previous value does not equal current value, execute uh an API call, right? I mean, why that that should be all it takes, I would think. Had to get a drink of uh water there. Um but yeah, I mean it's that that's like where I think um I I see a lot of people jumping to the dependency array, but in reality, you don't need it ever, because you could just have an if statement. Now uh there's one slight side case for that, um, uh which I'm we can talk about later. But in general, you you only really need to know that much about use effect, and you can use it to do everything you need to do. Um now we'll get into the the big thing, which is the dependency array now. So when um React was creating use effect, they added this dependency array as a performance optimization. Because I I I don't know if they've said this directly, but my interpretation is somewhere in the React code base they had just um a just a component that was just rendering a ton. And in fact, so much that the slight overhead of queuing up the use effect function and running it later was causing a bottleneck. And so they were like, okay, if we know if React knows the um values that might change inside of the use effect code, and we can know whether or not those values have changed, we can decide whether or not we should execute the use effect. So all it originally was was a performance optimization so that in my opinion, a super rare case, like in fact, I've never seen a case where this would be actually be needed for performance reasons. Like there's the cost of using a use effect is super low compared to almost everything else we do. It's just not a major thing. But I'm guessing at Facebook's scale, and they've got they've said they have like hundreds of thousands of components. You know, they ran into a case where they needed something like this. Um that was the original intent. Um it was also noted by them that what they wanted originally was for the system to automatically figure out the values that might change and track those for you. So you don't tell it what is what is in this thing. You don't use a dependency array, it just figures it out. And I don't know if they didn't have the resources to invest in a tool to do that, or if it's for some reason in JavaScript not currently possible or something. I don't know what the actual technical reasons are, but in the end, they tacked this dependency array onto the use effect call. And with that, you can basically all you're doing is telling the system, like, hey, these are the values inside my use effect code that could theoretically change. Now it's extremely important that you specify all the values in that dependency array inside of your use effect code that could change. Because like uh if you don't, you could actually get a different result depending on how your component renders. And that is not at all what Facebook wants with React. They want it to be a function, uh more like functional reactive programming style where you give it the same input, you get the same output. Um, and if you don't specify the dependency array correctly, you are breaking that contract essentially. And there's no guarantee anymore what's gonna happen. Um, and so so I think like there's a lot of people that use the dependency array, you know, not as performance optimization, and they might not be aware of the consequences of not fully specifying that dependency array. Um and also I think to me, the big big thing is there's just literally no reason to use it. If if you can put an if statement inside your use effect and it works equally well, there's no reason to use this weird performance optimization. Um it's just not it, it's no, there's no benefit, in my opinion. Um so I'm I'm curious uh what you think now, Austin. I know we've talked about this you know personally before, so maybe it's not new, but but what are your thoughts on this?

SPEAKER_00

So pretty much the what you're saying is the dependency array is not needed if you properly specify the conditions within the use effect on like that should trigger the effects within it. So say you um say you have like a screen where you're viewing something and the user interacts with something and it updates the thing on the server, and then the server uh sends back a new thing. So therefore, that new thing should uh maybe there's some transformation that needs to happen so that it can populate the page data properly. Um with dependency array, you could add thing inside of it uh and it would ensure that it only triggers when that thing is changed. But you're saying that you don't need the dependency array if you compare the previous thing to the current thing within the um within the the use effect conditionally. That's where uh I mentioned earlier the there doesn't seem to be a very standardized process on that uh storing the um the previous one because like I said, that used to be out of the box with the uh the prior like component did mount, you had or component did update, you had the access to the previous things. So in order to do that, you would just need like to specify like um you'd need to create a ref uh that stores the previous thing before the use effect and then uses that as the comparison uh to eliminate the dependency array.

SPEAKER_01

Yeah. Uh you you're actually um bringing up exactly what I was gonna say next is like if you think about this in terms of like a class-based component, you would just use an if statement. That's literally what everyone did, right? If previous value does not equal current value, and React passed you that previous value, and it was natural and it made sense. Now, I don't understand how this got started, but it got started that people were using that dependency array to implicitly get access to the previous value. That's what people are usually using it for is I only want to run this effect when this, you know, the value of this variable changes. What they're really saying implicitly is compare the previous and the current value, and if they're different, run this code. Um and that's not, I mean, sure, you can use you can't you can use it that way. That's not what Facebook originally intended it to be for, but you can use it that way. Now, you've also talked about like I think the main problem and what I was gonna talk about in terms of like use effect in sort of the ecosystem itself, which is there was no straightforward, sort of easy built-in way to get the previous value in hooks. And in my head, I'm thinking like, okay, is there like a hook we can provide provide or something? Or some can we make an API that like makes this as clean as it was with classes? I don't know why this didn't happen on a larger scale, but it seems like what happened instead was everyone was just like, oh, we can use a dependency array to implicitly do this. Um and and so I think that's now why we're in a situation where in most React code bases you'll see people using the dependency array all the time incorrectly. What they do is they specify two values they want to track in terms of whether or not they've changed, and then they leave out everything else from the dependency array that they're supposed to include, which might be function functions and state from the component. Like there's a lot of everything needs to be included because otherwise you could get different results. Um, and but but what happened, I think, is exactly what you said is you're like looking at it and you're like, well, how do I get access to the previous value? How do I do that? Like that's something it just happens to be something we do all the time, need to do in components. Is did this value change do something? Like that's just super common. And React did not come out with an easy way to do that. And so it seems like what happened was people just latched onto this dependency array as like a workaround, basically.

SPEAKER_00

Um and well, and I think also if you look in the the documents for the hooks effects, um, or if you like read any like any book, any literature, there's like the you're reading the examples. The examples say like, hey, you can if you only want this effect to change when count changes, uh, you add count to the thing, or if you want to only do it when uh like props.friend.id changes, it'll only run that. And that like ends the uh documentation, the main part, and then there's like a note, right, that has all of the stuff that we're talking about. Um like for some reason, even though this note is flagged in yellow and it's like, hey, it's kind of seen as an aside, right? Versus like, hey, this is actually like really important. Important and um these kind of things that feel like get skipped even though they're highlighted, right? Yeah.

SPEAKER_01

I you you actually bring up like my other major point about the faith failings of Facebook, I guess, in this regard. Like I think number one, if you provide a good API that just naturally does what you need, people don't really need to look at the docs so much and figure out this technical stuff. But if you're not providing that, providing really good documentation that's accurate and presents things in like the right order is paramount. And that definitely didn't happen because I have met, I don't know if I've met any React programmers yet that were that knew or read that flagged yellow part, you know? Like it just nobody reads it. And so to me, it's like it's super critical to read that because it really matters, but nobody's reading it. So like that's a big failing of the documentation, big failing of the API, and then of the documentation. And so I think what happened was uh the first adopters got into that um you know, got into writing this code and came up with this dependency array hack and put it in all the original material. And so now, like you said, it's everywhere. Everything you read tells you this is how you do it. And so, like, you know, in none of this am I blaming anyone that's actually using it this way or whatever, you know, like I'm more just trying to be like, hey, this is maybe not the best way to do it. And you know, I think, you know, I I've had I got into a big discussion with a coworker once about this who was basically like, it's not actually a big deal because you know, if I don't include a function inside that dependency array, it's fine because I'm never gonna change that function. You know, we program in a function pure functionally pure style. So, you know, in a sense, it's kind of been okay for the React community to abuse this because people have been generally programming in a functionally pure style, it seems like. And so we generally haven't seen the negative side effects. And at this point, it's so widespread, React probably can't ever change the behavior, you know. So, so maybe we will never see major, you know, negative side effects to using it wrong. Um, but I think that like ties into you know another part of this topic, which is like we need to stop thinking of use effect as the way to accomplish everything and start thinking of higher level ways to do things. And I think a really good way to do that, uh a really good example would be a use once hook. So in in class-based components, you had like component did mount. Your use once would be like that. Um, functional components technically don't have a mounting stage, so that's why I don't call it on or use on mount or whatever. But like I would call it use once, and everyone that you know wants to do something only the first time the component is loaded or mounted or whatever you want to call it would use that. They wouldn't reach for use effect or or other things like that. Like the case where you want to only run your um effect when uh value changes from the previous value, it shouldn't even require an if statement or looking at the previous value. Kind of like how we're using the dependency array. That's actually in a way a good design. It just wasn't made for that internally. So I made a hook called use conditional effect, which internally just uses an if statement. It doesn't even need to use a dependency array, but it handles that exact way that people are using use effect. But even that I think is still pretty low level. What I would like to see is people to start thinking like, okay, what is this effect I'm actually doing? Like maybe there's like a use API call effect, or which a lot of places have that they might make it more specific, like use, you know, um, item or whatever. And that's great. I think that's a great way to do it. Um, but I think in general, what I see is people just reaching for use effect all the time. And even Facebook has said many times, we did not intend use effect to be something people use. They thought people if they thought it was just gonna be like in libraries. So, like a maybe a library provides a bunch of hooks that are more descriptive and tailored for your situation. Uh, and for that has kind of happened, you can kind of find stuff like that, but it just hasn't proliferated in the greater React ecosystem. Everyone uses use effect all the time, and I think that's a a real shame. Like, I think we could do a lot better, and I think it would make a lot cleaner code. Like, what's better to read use once or use effect, and then you have a big block of code, and then you have to read down to the dependency array. And we all kind of know what it means now, but it's still not near as clean as just use once, you know.

SPEAKER_00

It makes me think maybe the solution is to recreate those component-based functions like component did update uh as hooks themselves, where you pass in like the certain things.

SPEAKER_01

Why not?

SPEAKER_00

It's funny, it's like we got away from those methods just to recreate them. I mean, but I think that's what's cool about hooks in general, is like you can you have access to the ability to create those life cycle hooks on your own with these things.

SPEAKER_01

Exactly. I think it's definitely an improvement. Um, and I don't know if I would actually say we want to do exactly like what we did in class-based components, because to me that's still kind of low level. But like in general, providing the ability to do those things totally makes sense to me in like a clean API. Like, um, there's a I don't I forget if it's actually from Facebook's website or where it's linked to, but there's like the use previous hook, which will get you the previous value of something. Things like that make way, way more sense to me. Like, stop using use effect all over. It's usually not the best way to do it. Um, there's usually a better way to do it. And and I think that ties into another uh point I had here, which was not necessarily related to the dependency array, in fact, not really at all, but another abuse of use effect where I'm seeing it all over, which is like um a common example would be there's an event handler. Like I click a button, and that button updates state in my component, and then I make a use effect with a like never mind, it does use the dependency array. You'll have the dependency array listening for a change in that value, and then kicking off like an API call or whatever you need to do. And you could just make all of that happen inside of the event handler itself. In fact, an event handler in React is essentially a use-effectable area, like you can do affecting things inside an event handler. Um, and I don't know if people don't remember that or it's just they're so used to using use effect everywhere that they just don't think of it that way. But it creates really sort of like decoupled spaghetti code for no reason, and I think could also be a source of bugs because now you're like sort of separating like that's gonna run in like two different renders. It's not even gonna run in the same render. You could get state intermediate state you weren't expecting, like it just opens up a whole lot of stuff. But um, and I know you know you you and I Austin have talked about about this, but like I literally see it with everyone's code everywhere. Like, I just everyone does it, and I it's another pattern that I'm like, it doesn't need to exist, we can do better, you know.

SPEAKER_00

Yeah, I mean, uh I think that this episode was kind of spawned off of uh some of the code reviews we were doing with each other and the questions I was coming up with and realizing that I was using them wrong. Uh, in your explanation in the beginning, where you're talking about how, hey, everything will render and then the effects will kick off. How many um multiple re-renders are being run because of this use effect paradigm is happening uh more calmly now. Um yeah, the I think like two days ago you were reviewing some code I'd written and I was running, I was running something in it, I was like updating the state in the event handler, and then I instead of just saying like, hey, I have this new value that I got from the event handler, instead of just using that to run some other functions that I needed to run, um, I set the state and then I put that into the uh use effect dependency array. And so theoretically it was like rendering two times or even multiple times because uh after that, because the um the the use effect also called uh uh multiple other hooks, right, that could have spawned re-renders on their own for for no no real reason other than just it's kind of like once you're uh if you're a carpenter, right? Uh everything looks like a nail.

SPEAKER_01

Yeah, exactly. Yeah.

SPEAKER_00

And I feel like that's how I have like evolved since hooks came out, you know, because uh I feel like it's also a problem of programming culture is um if you're not like keeping up with what is coming out and what is like the newest latest stuff, like there's a chance you could lose your job or not be hireable, right? So you're like, oh, I have to get good at hooks, I have to like prove that I know how to use effect properly, and then you just start using it everywhere and perhaps improperly out of for the wrong reasons. So you know we put ourselves in an interesting situation with this.

SPEAKER_01

Yeah, absolutely. And I really like how you brought up the sort of larger context of how this is probably happening as an industry, is just the constant, you know, sort of it's almost like a a constant ageism uh in the industry of you gotta always be learning the newest thing, whatever. Um and I I do want to say on that, like the upside of it is if you can learn the like fundamentals, what I call the fundamentals of writing good code, um, you can sort of escape some of that. Um, because like when these new things will come out, you'll be able to recognize the anti-patterns, you know, a lot easier. Maybe not right away, but it's a lot easier, you know. And you know, this is I guess sort of an aside, but like what I spent a long time doing, and maybe this wasn't the perfect use of my type, but I enjoyed it. With some, I would study, I would literally study code, and maybe not even in languages I knew, but I would look for patterns into how to make, I would either take like just general code and figure out how can I make this into less code, or I would look at somebody that I knew wrote like um really clean, um well-designed code. Um, and I would try to make it even better. And so, like always obsessing over, like this isn't something you can necessarily do at your job, but like taking that as an exercise, like, okay, how do I find the fundamental things that are occurring in this piece of code? And how do I represent that in the most concise way possible? I guess that's how I always think of it is like, how do I make this more concise? Can I make this more concise? If I defined a new primitive or a new way of thinking about it, would it allow me to rewrite this in a more concise way? And that really got my brain into this like long-term mode of always analyzing everything to be like, okay, what is the fundamental thing that this code is doing? And what is the best way to represent that bit of code? And I think if you can get better at doing things like that, it'll be easier when you pick up new technologies to find the the best ways to write code in it.

SPEAKER_00

I don't know if that made any sense, but no, no, it's uh definitely smart and not like you. I think the biggest thing is uh there it's like that triangle of like, do you want it fast, do you want it cheap, or do you want it like bug free, right? And uh the bug free solution, we you would take time to do more stuff like that where you actually read through and properly uh make sure everything is like really optimized. But a lot of the times that is usually done as needed when something comes up and something is broken, and it's like how far have you gone writing incorrectly before you started to learn how to write correctly, and then your correct writing is not even necessarily correct because you're just fixing something that was broken from the beginning, and it doesn't mean that it will it will create a good solution, it will just fix a crappy one, right? So to be able to take time and make time to do that to properly to study, uh instead of just outputting, outputting, outputting, I think it's a really smart thing to do and needs to be done more.

SPEAKER_01

Yeah, and like I I think um you also touch on like I might not necessarily like be doing that with the code I'm writing, you know, day to day, um, just because we it doesn't fit in that triangle, you know, like we we don't necessarily have that, but like as long as you can at some point train your brain to think that way, you'll actually be able to cut through a lot of the stuff that might trip you up or the extra code you might write, just because your brain is recognizing patterns. Um, and that that's what a lot of it is. Like, okay, this seems like an anti-pattern because I'm spreading related code out. And then in my brain, I'm like, oh, okay, is there a better way to do this? Do I need to do this? That's how I even figured out that issue we were talking about with the event handlers and stuff. Is at first I was in React mode and my brain's just like, oh yeah, it looks good. And then I'm like, but wait a minute, aren't these like related bits of code? Is there some way we can like keep them together? And then it's like, oh, the light bulb turns on, you know, like like maybe I wasn't necessarily thinking of that exact thing in my head, but at least I was like looking it for that pattern in general, you know.

SPEAKER_00

Well, I think in a way, the use effects uh created this problem because like as software developers, we like to uh perhaps decouple code to make sure it's not like a huge big block you're reading and like abstract things to other places. And so the use effects kind of like created a really simple way to do that to the point where it's like, wait, like why like is this even does this even make sense? Like doing it this way, like we we used it as a tool to to do something to like encapsulate things, and then it's just making things jump all around. Yeah, it's like oh wait, but now this is the new the quote new normal, right?

SPEAKER_01

Yeah, I mean that I think it still ties into what we were saying too, is like really what I was talking about is abstractions, conciseness and abstractions, you know, it's understanding where where and how is the best way to to do abstractions, and you know, that's something that you can learn on a general level and just apply sort of all over, but it definitely takes uh you know work and and practice. Um and definitely not something that seems to be prioritized, you know, at your normal workplace. Um, in fact, it it almost never is, even though it's probably one of the biggest um things when it comes to code quality and maintenance and stuff. But anyways, I think I think there's a a great uh another episode in here where we could talk about that kind of thing. Um uh but for for now I think we're gonna um start winding down our our use effect here. And there's I mean, I thought actually like I was like, oh, we can blow through this pretty quickly, but now I'm like, oh, there's so much to say about it, you know, we might have to have another episode on it. Maybe it's too boring for people. But if you like it, if you want to hear even more, you know, let us know.

SPEAKER_00

Um, I think that's why people are listening to the podcast because they do like this stuff.

SPEAKER_01

Yeah, that's true. Um, yeah, so I think um we're gonna call it a day here. Again, if you want to send us feedback or questions, it's the React Show at Owlcreek.studio. Um, and if you want to check us out, uh it's owlcreek.studio. Uh you can you can pick up a free copy of Foundations of High Performance React. It's is it free right now if you sign up for the mailing list, Austin? I forget.

SPEAKER_00

Yeah, if you uh on the main page, owlcreek.studio, if you scroll down, there's a big uh banner that shows the the book cover, and there's like a little email sign up you just put in your name and email, you click send, and within a few minutes, hopefully, uh you'll get a copy uh via email uh that has a link to Lean Pub with a 100% off discount. But I don't know if we're gonna keep that around forever, the 100% off. Um so grab a copy while it lasts.

SPEAKER_01

Yeah, sounds good. All right. Well, thank you, Austin, and it's been great having you on again. Um we're we'll uh we'll be back soon, and uh yeah, that's it. See ya. See ya.