SPEAKER_01

Welcome to the React Show! As we all know by now, everything we experience on this planet has a life cycle. And React Components are no exception. But do you know the life cycle? Do you know when the DOM will be updated? Is that before or after your effects are run? Do you know how to cleanly create your own life cycle methods like On Mount? Today, we're going to dive deep into the life of React Component and answer all of your most profound component life cycle questions. 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 current of the journey. Of the journey we are all bound together in. 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, in a better world together. The React Show is a weekly podcast brought to you from occupied Miwok 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 31. We're with Owl Creek, an organization focused on React and React education, and today we'll be focused on React function components. We'll make some comparisons to class-based components of Y old times, but primarily our focus is on moving forward. And that means function components. And speaking of moving forward, I'm your host, Thomas, with my wonderful, amazing co-host Austin. Hey Austin, welcome to the show.

SPEAKER_02

Thanks for having me as usual, Thomas, and thank you everyone for showing up today for another episode. We're on 31 today, right? That's pretty exciting, huh? We we've come a long way in a few months.

SPEAKER_01

Yes, totally true. And uh, you know, as you may know, um, you know, coming a long way, this show actually originated from Owl Creek, uh, which is our React consulting firm. And uh while we will continue offering consulting services, Austin and I have learned through this podcast that what we really love even more is helping people and educating people. And there's nothing more important to us than you. All of you listening and supporting us and learning together with us. And yeah, so we just wanted to thank you for being here with us and being a part of this community. You you really are uh what drives us to continue to do this more than anything else. We want to hear from you. What topics are you interested in? What formats do you learn best from? Is there anything you know we've touched on that you want to learn more about or any other feedback you might have? We'd absolutely love to hear from you. Uh and and you can email us uh feedback directly at the ReactShow.studio. You can tweet me at Thomas Finthscons. Um, and I'll include that information in the podcast description. We'd love to hear from you, and you mean the world to us. Um and well, if you don't know this yet, you will soon. The other part of the world that means a lot to us is React and React Components. Um so I thought like we could start um by uh just I'll just tell a story about React Components that I think is a great launch uh into this deep dive. Um yeah, so so that story is uh it starts with actually testing um and a testing framework um that I often use to do integration testing called Cypress. Uh actually Austin and I both often use. Um and we had a test where it kept failing, and um basically that the framework is like, hey, you gave me this DOM component to you know do some tests on, but it's not attached to the DOM anymore. It's detached from the DOM. I can't do any, you know, like this doesn't make sense, you got something wrong. Um, and I was kind of like really struggling to figure out what was going on. I couldn't figure out why this is being detached from the DOM. You know, and and so I was trying all these different hacks, you know, whatever, trying to figure it out. Eventually, I I realized that the thing that was causing a problem was actually coming from uh a component that was defined inside of another component. And I I don't know how common this is in the React world. Um, in fact, I I haven't seen it very much, but like in my brain, like lexical scoping is a beautiful thing because I can scope things to where I think they belong. So if I'm like, hey, this is a helper component and I only use it inside of this other component, it should live inside of that component. It shouldn't be some globally available function or component, you know? Um, so like that made sense logically in my head. So I would define components like that often. Well, apparently I didn't know the React component lifecycle as well as I thought, because what was happening is every time that component was re-rendering, uh it was um the the parent component, it was mounting or unmounting and remounting all of the components defined inside of it. And so that's why I was running into these detached DOM issues is even though the DOM element wasn't being changed between re-renders, so it should have been preserved, like React shouldn't have been changing that DOM element, it was because React was going, hey, this is actually a new component definition. Like it thinks the definition itself has changed. And so it's like, oh, I need to re-render, I need to remount, and and all this stuff. Um, so yeah, that's the um sort of a wake-up call for me of like, hey, I need to make sure I understand this better. Um, but yeah, I'm so I'm curious, Austin. Uh, have you had any like maybe similar experience or experience where not knowing about the component lifecycle caused you problems in some way?

SPEAKER_02

Uh well I think a lot of what we've touched on, uh what you just touched on now with the Cypress testing and the um these components not being mounted properly at the time that they were being tested, we we've kind of shared that pain together uh on that project. Um and I've also like I think the main thing with life cycle stuff comes in that I've really struggled with is especially when like I'm still always finding that I'm constantly hunting down extra re-renders uh in in my code. Like typically when I make a like a first implementation of something, uh especially like when when it's not like a brand new clean implementation, you are like leveraging existing components or things that like or libraries to complete a task, and I often find myself looking and I'm like, why is this thing re-rendering like six or seven times? And it's all like the the larger your project gets and the more like paradigms that you continue to re-implement or components you continue to re-implement, uh, these things can be harder to hunt down. Like, where where do they actually come from, these re-renders? Um, and I really hope in this episode we can figure out a way how to like hunt these down from the beginning with some best practices so that when we begin implementing with pre-existing code, uh leveraging our component library, we're not like stuck with the same like hunting down re-renders, etc. all the time.

SPEAKER_01

Yeah. Um, you know, I hadn't hadn't really uh planned to talk specifically about that, but that doesn't mean we shouldn't.

SPEAKER_02

Um well we don't have to talk about that in general, but like I think I I would love to by the end have some like good ideas of how uh how to avoid these like things, these pains that we're having.

SPEAKER_01

Yeah, I mean I guess that's what I was gonna say is like um understanding this component lifecycle should hopefully at least help with that. Um I don't you know I need to I guess I need to think about this a little bit more, but like um it it it does, I mean it does tie into it, um, absolutely. So so I think it was great you brought that up. Um yeah, so I think uh the first thing then is you know, defining, you know, what actually is a component um from React perspective, like what makes something a component, what is what is involved in that? And um the truth is, at least when we're talking about our functional uh React components, our function components, sorry, not functional, um it is uh nothing more than a function with bookkeeping. So from React's perspective, it's like, hey, I have a component, it's named you know, XYZ component, and it stores a reference to the definition of that component, basically a reference to the function that you've defined. And then it might store, you know, like it's uh it'll it'll store things like the current use state values and stuff like that. It'll associate that with your function definition. Um and and I think it, you know, it'll store its like potentially its place in the component tree or something like that. But that's really all it is. It's it's just React probably has some data structure that's like, you know, function name, XYZ, function definition, and a reference to the function, function state, and a array of whatever the current use state values are. Um that's actually really all it I mean, there's probably some more nuance to some of this, but ultimately that's like basically all it is. Um yeah, I'm not sure. Did you did you know that, Austin? Or did you just have this sort of like mental black box image in your head where you're like, it's a thing, and I use it.

SPEAKER_02

Yeah, pretty much. Pretty much.

SPEAKER_01

Yeah, I mean, that's that's fine. Nothing wrong with that. I mean, a good abstraction, that's all it needs to be, right? Um, you shouldn't need to know this stuff.

SPEAKER_02

Well, I mean, that's where we we talk about like the two types of software developers is the ones driving the cars and the ones in the in the garage. But I mean at a point, like sure, you can you want to be driving and you're like you made it halfway down your road trip, and then the car breaks down on the side of the road and you uh you do have to open the hood. Uh so I love these chances to get a sneak peek underneath.

SPEAKER_01

Yeah, or even like maybe uh if you want to drive your car optimally, you might want to know you know when when is the right time to shift? Maybe maybe you're shifting manually, and if you don't know how the car works, you might not know that. You you might be able to kind of figure it out by guessing and checking, basically, but like it'll be more efficient if you understand. Yeah, totally.

SPEAKER_02

Um well, I like that. So there's now like yeah, so there's like this manual is kind of like a bridge for the the engineer who made the car and the guy or girl or whoever is now driving it, right? And uh I like the analogy about shifting and stuff. That's that makes a lot of sense. Yeah, you don't just want to hop in the car without your license, right?

SPEAKER_01

Yeah, exactly. Um, yeah, so now that we know like what it what a component is from the uh person under the hood and the uh you know bridging this gap here, um yeah, let's look at I think the life cycle, you know, the whole point of this this show today. Um so what is the life cycle of a component? And I break it down into four main uh I want to say components, but that's confusing, four main parts. Um so if you look this up online, you might see a little bit different uh views on this, but hey, these are my views, and I'm giving to you, giving them to you here. Uh so the first thing is mounting. So uh your your component gets mounted. We've all heard this term probably, but hey, what does that mean? Um basically it means that React is setting things up. Um, it's setting up the default state and props, that kind of thing. Um and uh I don't usually consider the the first render to be a part of mounting. I consider it to just be React loading this thing up and putting it into its internal data structures and and setting up the state and props, um, that kind of thing. Um the the other part of this would be um, you know, if there's connections that need to be made to like uh the DOM or React's internal like um DOM tree, uh it would probably be set up in this phase. And that's why if you are doing unmounting and remounting, you're going to have new DOM components. Um so the next phase is the one that we're usually working with mostly, which is the updating phase. This is um when React runs actually runs your function. Um the first time, obviously, it will use your default state and props, but after that, this is you know where it uses whatever the current state and prop values are. Um and this is uh you know where it runs yeah, runs the body of your function, your your component. Um But the question then becomes like you you return an output, you know what does React do with that output, and also how does that interact with like your effects? So in the updating phase, um React does not like run your effects, um, but it does update the DOM. So it goes through the DOM, it goes through your component tree, takes the output, updates the DOM, and it runs uh use layout effects. I'm not sure if you're familiar with this, it's another like hook you can use. Um there's use effects, which we usually use. I'll I'll talk about the distinction more later, but there is a distinction already, and that is that use layout effects is being run sort of coupled with the updating phase. Um, I know Austin, you had said you you weren't really familiar with use layout effects. Is that true?

SPEAKER_02

Yeah, I've actually never used it, and I never even read the documentation about it, to be honest. I've seen it before, and I was just like, look, looks looks good in the code review to me.

SPEAKER_01

Like yeah, yeah. Well, um, yeah, that's actually perfectly fine. I I don't think I've ever found a case where I needed to use it. So I mean there are cases, and and we'll talk it's actually this this is the perfect um opportunity in this show to talk about it, and we will talk about it. Um yeah, so we're gonna discuss more about that. But that's the updating phase. Did you have any like more questions about what's in the updating phase, or does that sound pretty straightforward to you?

SPEAKER_02

Yeah, um, I I think everything's pretty pretty clear on that.

SPEAKER_01

Okay. Um, so next we have what I call post-updating or effects, and this is where you might see divergence from what what you you know uh a Google search will show you. And I think a lot of things are wrong about how they they do this. They don't actually understand how React works. But the way it works is um other than you know, use layout effect, um the way it works is you have your updating phase, React updates to the DOM. Now, after it's done doing all those DOM updates, it has a big queue of all of the effects that need to be run. And it just runs those in batch, essentially, after it's already done the DOM updates. And this is a super critical difference from class-based components. So in class-based components, they behaved like everything was a use layout effect where um after you like return your output from your component, React would then run that effect. And so it's sort of this um uh execute the body of my function, run the like effect or lifecycle. We call them lifecycle methods then. Um the difference in function-based components, if you use use effect, is that these steps are separated. And that means you can actually get much better performance because you're not getting this weird thing where like your effects, like if you had a slow effect on a class-based component, it would actually uh it could actually potentially make your renders look like sort of um delayed and partial and because they were sort of being intermixed with each other. The big change React made with use effect is that it all gets batched together and run after. So if you have slow use effects, it's not going to uh really impact your rendering near as much. It really won't. It could cause other like effect uh issues like delaying future renders potentially, but at least uh your UI should feel a lot more fluid and your update should feel more fluid. And so that's why like Austin, you're like, oh, I've never even like used use layout effect. That's probably a good thing because if use effect could do everything you needed it to do, um, it's absolutely a much better way to do it. Um, so yeah. Um, does that make sense? Our our post-updating is just basically running these effects in batch after we've done our updating to the DOM.

SPEAKER_02

Yes, and um this this whole concept of batching is not only applied to these effects, but also with state updates as well now in the newest version of React, right?

SPEAKER_01

Yeah, yeah. Um that's true. And and a lot of this is um I I believe, I'm not a hundred percent sure, but I'm pretty sure. I believe this is actually a lot of it's related to being able to start doing things concurrently. Because when we had all of our effects and our updates tied tightly together, it was I think basically not gonna be effective to do things concurrently. And so the React team, you know, has to find a way to sort of you know create these discrete units of work separate from each other. And and so that's my suspicion is like the state updates, the uh use effect, all of this being you know separated from our components generating its its render output uh and updating the DOM is is pretty important for moving towards a concurrent uh system. So I think that's pretty cool. Um yeah, go on.

SPEAKER_02

No, I I'm done. I'm I'm just like shaking my head. It's it is cool. No, I um it it's uh as we I I think the main thing is as we like a lot of people don't realize how these things are going to help out until you realize like the problems you're facing and how these these paradigms are like shifting to help alleviate these problems. Um do you feel like the the stuff that we're talking about today with like your your case with the component being inside of a component, will is that just like how we have to do them external to their parent component, like as globally available components, or do you think that there's anything that will uh change in the future that'll allow to allow you to write it in this uh lexical paradigm that you had previously outside of React?

SPEAKER_01

Uh that's a fantastic question, and my gut instinct is no, that's not going to change. And we will talk about that. I'm I'm adding that to the notes now to talk about because that's actually really, I think, a fascinating uh uh uh subject. Um yeah, so right before we get to that, we'll just finish the last phase, which is the unmounting phase. And um this would be like when React actually like tears down your um the DOM elements that are related to your component, like removes them from the DOM, uh, cleans up your event handlers like the that have been set up in JavaScript that React sets up for you. And it's when React runs your effect hook cleanups. So anything that has a cleanup, you know, like with use effect, you can return a function and that's a cleanup. You know, um they they will get run here. Now that's not the only time they get run, but they do get run there. Um they they also will um so this is this is actually something that I don't know if people know necessarily, but you know, in the um use effect stage, um usually if you don't specify a def uh sorry in the post-updating slash effect stage, um if you don't specify a dependency array on use effect, it runs for every render, right? Well, that is the same as your your cleanup. Your cleanup gets run in between every time your effect gets run. Um so if you have a dependency array, it's still gonna run your cleanup, but less you know, the same amount of times as it runs the effect itself. Now Now it always makes sure that when you unmount, we run that cleanup. And that's the more important part. Usually we probably don't want it cleaning up all the time. That's just how it works. The unmounting, though, we make sure that we always clean up, we have no memory leaks, you know, we're not listening for an event and not unregistering that event listener, that type of thing. So that's the unmounting phase. And those are the four life cycle phases. So mounting, updating, post-updating, and unmounting. Yeah, so I think now that we have that great foundation for understanding how this works, we can talk about um the thing that Austin was just mentioning related to the story I I gave. And that is how does React track your components definition? So have you ever um Austin? Here, here's a good question for you. It should be easy, don't worry. So have you ever done like uh const uh xyz equals and then you you did like um a function. So uh, you know, with an arrow function or whatever. Yeah, you've done that, right? I mean we all have.

SPEAKER_02

Quite often.

SPEAKER_01

Yeah. Um, so that's essentially all React is gonna be doing. It's gonna just be like const xyz component equals, and then a reference to the function you've defined. Um and what it will do at some point, I don't actually know when it does this, but at some point it's going to say um, is the like globally defined definition for this component or whatever, or this thing a part of my tree? Is it the same? Does it point to the same thing as the thing I stored? And if it's different, it's going to unmount your component and remount it. So that's all it does. It just checks to see if the definition essentially of some data structure variable or something has changed. Um and that's why I don't suspect um we're gonna be able to do what I want to do anytime soon. Because um you gotta think about it this way. Every time that parent function renders, it's defining a new function definition essentially for that internal component. And so every single time React's gonna be like, oh, that function definition changed. Even though it's the same exact function, you know, it's it's a new instance of a function. And so it's pointing to a different place in memory, and so they're not gonna be equal anymore. Now, how would you like how would you get around that? Like, like how would you make it work the way that we want it to work? Um, I think that's not a uh a straightforward um thing to answer because what we're really saying is don't unmount and remount a component unless the actual definition of that component has changed. You know, so you'd have to like track the essentially the source of the function and make sure the entire source of the function is the same as before. Um that that's one that's like one way you could automatically do it, which I don't even know if you could do. Another thing you could do.

SPEAKER_02

Like the function itself, or like I'm not sure.

SPEAKER_01

Yeah, you could, yes. So if you wanted to use this inside a uh a function, you're absolutely right. That would be the way to do it. I don't I've never tried that. I if React is happy with that, but that would be the way you'd have to do it, is store it as a ref. Um but like I don't know, that sounds really ugly. Like every time you want to use the component, you have to be like component dot current.

SPEAKER_00

Right.

SPEAKER_01

Yeah.

SPEAKER_02

Maybe it's gonna be the new paradigm that after everyone listens to this show, now we're gonna see it pop up all over the place. They'll be like, those owl creek fools.

SPEAKER_00

I think it'd be awesome, but also so ugly.

SPEAKER_01

Um, yeah. I love it. I'm I'm glad you asked that because I think it does help. At least I hope that helps explain better um, you know, how that works. Um yeah, so uh now that we've cleaned up our understanding of what a React component is in its life cycle, um, yeah, I thought it was time to learn about how to actually use that knowledge to effectively manage and utilize the life cycle of component. Basically, get into Austin's favorite part of the show. Austin's giving me a giant smile, so I know I'm on the right track.

SPEAKER_02

Where I just listen and shake my head and uh hope that you don't spring any crazy questions on me.

SPEAKER_00

Yeah, exactly.

SPEAKER_02

All right, well, I'm ready for them.

SPEAKER_01

All right. Um, so yeah, uh we're gonna start with uh a question for Austin, and I know this this might I'm just kidding, actually. No, um I was gonna start by like uh talking about I think the thing that most people um run into in regards to this, and that is how do you like create and set up life cycle methods um with use effect properly? So probably the first thing people want to know is and actually probably people think they already know the answer to this, but how do you create um like yeah, we'll just start with this. How do you create an on-mount effect or like uh like an effect that runs when your component is mounted? Um and here's where I'll I'll give Austin the uh the difficult question. What do you think? How do you do this?

SPEAKER_02

Um well, I mean, I know that with use effect, well, on on mount, so we're talking about running an effect after the initial render, right? So it would be tech like I know currently people do the use effect uh with an empty uh array, which is uh technically not proper to do it like on mount the the very first time. Um as to create your own like proper implementation, I feel like you'd have to make a hook where you have some sort of toggle where you say like, hey, this has run one time already, um and like do uh use effect conditional within that hook to run based on that condition, uh if it's false, and then after you run it, you would set that it ran to true. Um and then you would have to somehow make sure that when that you would have to make sure that on unmounting of the component that used the this hook that you clean up the the has mounted or not state. Does that sound right?

SPEAKER_01

Yeah, yeah, absolutely. You're on the right track for sure. Austin wiping his brow. Um mopping. Mopping, yes, there we go. Mopping. I guess it's it's even more intense than I thought, not just wiping. Yes. Um yeah, so actually where I'm gonna start is the use layout effect versus uh use effect. So I think a lot of people might are be coming from a class background, like they used classes, and those had a um, I don't know if it was, was it called on mount? I forget. There was some lifecycle method.

SPEAKER_02

I don't know why I'm on uh component did mount. Yes, yes, that's yeah, there's like commote component did mount, will mount, will update, will unmount, did unmount.

SPEAKER_01

So component did mount, I believe, is the one that a lot of times people are trying to replicate. They're like, hey, once my component is is there, I want to do stuff with it, right? Um yeah, yeah, go on.

SPEAKER_02

That's what I feel like I feel like that's 80 plus percent of most use effect cases. Uh I think that's it. And then when when you're using them otherwise, I think it's maybe there there's a good chance that 80% of them are done incorrectly too.

SPEAKER_01

Yeah.

SPEAKER_02

Which we've talked about before.

SPEAKER_01

Yep. Yeah, so this you probably didn't know until this uh show, though, is uh you can't use use effect to replicate um component did mount from a class-based component. Now, a lot of you might be wondering why not. Like, I don't understand. Um and it just has to do with the distinction from when use effect runs, when when your use effect code runs. Um in the class-based component, uh your component did mount would actually run uh immediately after you're like done rendering your component and it gets um updated in the DOM, and it's not waiting until your entire like tree of components has rendered. And so that's where use layout effect comes in. Like if you're actually porting over code from a class-based system and you want to make things like the same, then you should use use layout effect, not use effect. Um, and and that's just because use layout, like you depending on what you're doing inside your component did mount, you might get different results if you use use effect. Um, so I think that's important to call out. But like Austin was saying, Austin's like, I've only ever used use effect and it's been fine. So clearly we don't necessarily need use layout effect for our on mount you know hook or whatever, right? Um I will note though, Austin, so in in uh a contract that we were recently a part of, um I had an issue. I didn't know about this distinction, and I was trying to port a class-based component over to a function-based component. And I later found out that the issue was that I was using use effect instead of use layout effect. So like it does matter if you're doing that kind of thing. But if you're working with just a um, you know, like green field code, you're you're writing a new component. Um, there's almost never a reason you'd want to use layout effect in this case. Um, unless you basically unless you need to like measure pixels that are getting painted or something. That's about the only time you like if you actually need it to be synchronous. Um, and you generally don't want it to be that way. So, yeah, well, I would say generally you'd want to start with use effect, even though it doesn't exactly replicate what we did in a class-based component. It's much better if you can do that. Now the next thing is, and Austin touched on this, is the dependency array. You know, here at the at the React show, we are very big on not incorrectly using it. Um, and so if you just use a d empty dependency array, but your effect has dependencies, that's not correct. And and I I won't approve your PR. Sorry. Um But uh we can talk about uh how to do it the right way, and Austin touched on this. So what I what I um utilize is something called use once. I don't call it you on mount or use on mount because uh I think the distinction with use layout effect is kind of makes it less about mounting and more about just running the first time. Um so that's why I call it use once, but you could call it anything along those lines. Um yeah, so it's it's pretty similar uh to um what you would what what Austin had said, and and basically you just need to use a a ref. And the thing about a ref is the first time that your ref gets used, it's gonna be undefined. So you can just use that to detect if it's the first run. If it is, set that ref to some true truthy value. Um, and then you'll be able to detect whether or not this effect has ever run and only run it once, just you know, a basic if statement. Does that does that sound good to you, Austin?

SPEAKER_02

Yeah, and it makes me think about like the naming conventions. Like, would it be why like of all of those life cycle um functions within a class component? Would it be wise to like call the uh these hooks like with the same um kind of naming convention, like on component did mount or something? Or does keeping it like use wants abstract it to be more useful in other cases? Or do you think that would be too too specific?

SPEAKER_01

I mean, I think it's a fantastic question. I just don't know that I have many answers. I I I'm still one trying to figure this out for myself. Um the way that I like to answer those questions is uh actually making those options available in my code base and seeing which ones I end up wanting to use, even if the definition is the same. The way that I look at it is I'm writing code which comes out naturally, which makes like the most natural sense to read and write and that kind of thing. So that would maybe be my approach to looking at it. Does that answer it?

SPEAKER_02

Yeah, yeah.

SPEAKER_01

Like, yeah, I don't know. I I I actually that's a really interesting. I think we should do that actually. Start start doing some experiments because getting that right can make a huge difference. And I don't know that use once is right. Um, maybe use on mount makes more sense to more people or reads better, you know. I I think there's certainly arguments for that.

SPEAKER_02

Yeah, I mean, I think there's just like uh use once can be kind of generally used uh in a lot of cases, and it's not necessarily like explicitly tied to mounting, um, but it kind of is directly. I I don't know.

SPEAKER_01

I mean that's why I originally wanted it to be used name named use once, and that's because it isn't tied to mounting. There's not a that's like if you wanted it to be tied to the actual mount, you would need to use layout effect. So that's where I was like, but then I'm like, well, maybe nobody cares. Maybe everybody is using use effect and it just makes more sense to call it use on mount. I don't know. What do you think?

SPEAKER_02

I I think that there's like some use to like adopting the naming conventions of the library that we're working in and trying to make it like it might make it more like broadly uh understandable because it'll be like, oh, that's what this is used for. But I mean, we're getting we're like splitting hairs at this point.

SPEAKER_01

I mean, we're splitting hairs, but I find it's actually pretty important to the like adoption of something. Like if people latch on to something, it they you know, it's the classic, you know, thing that I often get into with people. Why why don't more people use Lisp? Why do more people use more Java than Lisp?

SPEAKER_02

And it's like because they don't know.

SPEAKER_01

Well, I think it's like ties partially into this. Like the you know, having to learn things or things that seem further afield, uh you know, people might not latch on to it as much. And it so then it doesn't end up propagating as much. And you know, of course, there's a lot more to that whole discussion, but I often think of that and and when it comes to naming and stuff, because I think it does actually matter a lot.

SPEAKER_02

Yes, the this just makes me think of like I I just go back to like thinking about class components and how like you very explicitly have those those functions within your class that do that are like based on these life cycles. And it just makes me like think about how can we construct like exact replicas of all of those um within uh a hook-based environment and have it be like readable, understandable, and actually useful because I feel like hooks kinda we we lost a bit of the like some of those things within the class components, and they were like, well, we can do it with these functional hooks instead. And it's like like I know we've mentioned um like the all of these have like the previous props or the previous state available like within that context and able to like handle that, you would have to like store like you'd have to store your props in a like use previous hook where uh so it gets kind of like messy, and then like the that use previous hook, uh like previous props, like that won't necessarily be the what you expect within each of these hooks because it's like stored in one variable that you're referencing in all these lifecycle hooks potentially, and so then you maybe have to create multiple previous prop uh like hooks to for each of the life I I don't know, it sounds like we need to figure out a way how to do it. I I just don't feel like there's a clear definition of how to do it properly, right? And we're trying to figure that out right now. And these are just questions that come to my mind.

SPEAKER_01

Yeah, I I was just waiting for you to actually bring up the prev props and prev state.

SPEAKER_02

Well, yeah, that's like that's the main thing that's missing. Um, and I like with use effect, you like you're like, okay, I conditionally I want to run this effect on this thing, and then you have to create these prev props or prev state with uh use previous hook, and then do all these comparisons within there, and then you have to it's it's weird, like for example, this one ex uh I'm making a form where um in order to modify the way the form works, because it's within React Hook form, I have to rely on like the previous values of like the form. I can't use use state because React Hook form has its own like state management things. So I have like these form helper um um fields that aren't supposed to actually be in the form, but they're required to make the form dynamic. And then there's like cases where using React query, it pulls in the data I'm getting, and it's like, no, I actually don't want to run that hook on the very first time because it should like when it's undefined. So there's these like little things that are recurring with this paradigm that you have to like remember and implement manually every time that um when you go a month without having touched it, you forget, and then you spend 30 minutes to an hour, maybe like, oh yeah, yeah, I forgot I needed to add that in. But like we we just had a a pair session yesterday where I realized that as I was rubber ducking to you, and uh so yeah, yeah, sorry.

SPEAKER_01

Yeah, no, no, that that was great. Um I think I have two um main thoughts on that. Um the first is uh it you know, it when I I look at the code that I've written and uh I I actually can't I I find it incredibly rare that I need pre to to access the previous props or state. Um and I know that for a lot of people that's not the case. So I've been trying to figure out like why why why do I not need it as much? And I think it might so in your case, when you're talking about like dealing with this external library, um uh there's not much you can do. You just have to need that stuff. Um for a lot of cases, what I've found is that people are splitting up things that they change between like an event handler and an effect, or a request response and an effect, or and that's when you like start needing to access previous values. Um, but by default, you know, we close over the current values so we can always access those, right? And I don't know, just in my experience, I don't know if it's the way I write code or something, but I just literally barely ever run into this. Not that it's never happened, but um, I'm I don't know. I guess I'll have to explore this further because I know that for a lot of people this happens. And so for for those people, I I am totally on board uh with with Austin and figuring Figuring out, like, um, okay, so I guess I have two thoughts on this. One, yes, I think there is some value in and replicating some of these lifecycle methods, but and this is another reason why I called it use once instead of like use on mount or something. Um, I am thinking about hooks in a sort of higher level. Like I don't to me, the so so it's kind of funny. Um, the lifecycle methods from classes, I think, are a little bit higher level than hooks. Like, like Austin is saying, they provide some of these things like prev state and prev props by default, which you can still do via hooks. It's just you have to do more bookkeeping and more work manually, right? And you can make a hook to like sort of do all that for you, but it's not done. You know, basically hooks are lower level than those lifecycle methods. Whereas my brain, I'm like trying to be like three levels higher than that. Like, I don't want to like use once even is a very low-level hook for me. Like, I don't want to have to write that very often. That should be like maybe in some underlying library that I barely touch, you know? Like my hook should be more like um business, you know, related or or product related, you know, like that's the level that I'm usually thinking at. And so while I think there is value in defining these hooks, maybe for like when you're working on this library stuff or when you have to deal with like Austin's case, a library that just requires you to do deal with it. Um, I guess the reason I haven't invested more thought in this is more because of my brain's always a couple levels higher. Um, which I don't know if that's better or worse, because it's important to have good building blocks as well.

SPEAKER_02

Um, and so yeah, it doesn't sound very bottom-up, Thomas.

SPEAKER_01

Well, it is bottom-up. I just sort of skip a few levels, which might not be better. Um yeah, no, like the original version of my code might be based on like used once or something with used previous. And then I'm like, oh, actually, I can if I base it on this, you know, business type concept that my application's actually trying to deal with, I just sort of skip over needing those features anymore. That's what I'm saying. I know it's all kind of wishy-washy and black boxy, but um, yeah. So I do think we should talk about uh oh yeah, go on, Austin.

SPEAKER_02

Well, I I think you're you're you're right too, because you don't realize you need them until you're like, this isn't this isn't working the way I was expecting it with like this implementation. Then you have to like build them, and then these are like a byproduct of uh we have like a whole little hooks folder, like a common hooks folder that's got like 30 of these things in it that we've had to create to to manage stuff like this.

SPEAKER_01

Yeah, very true. Yeah, that's what I wanted to say is the next thing we can do is look at um you know doing those things. And we we talked about the on-mount um hook, right? Well, what other hooks are we looking to replicate from the life cycle methods? Like um, I think the other obvious, you know, corollary to that is on unmount, right? So oh go on.

SPEAKER_02

Yeah, un unmounting um and doing that properly really seems to be like an afterthought or not even a thought for a lot of things. Because there's there's a lot of times where you are you finish using a uh a component and then you like don't go back to it until like you maybe navigate the pages back and then it reloads that brand new, but that doesn't mean that the whole time that that component was still like running and persisting itself to some degree in the background, causing potential side effects while you're moving forward uh and you can miss these things. I feel like unmounting is definitely overlooked uh all the time.

SPEAKER_01

Yeah, so first of all, if your component is no longer being used, it will be unmounted. Um, the problem is, well, like I think what Austin is saying is if you're not cleaning up your component correctly, you'll be causing problems. And you often see warnings about this, right? Like uh this component, I forget what it is exactly, but basically it says, hey, you this might be a memory leak, right? Like you've seen those, right, Austin? Yeah. So the truth is, as far as I can tell, there is literally no way to do to have an on unmount. Like it just doesn't exist. There's no way to know that your component is being unmounted. But I I I mean, maybe there's a case, I don't know, there's probably a case where you need it, but I've never run into it. Um if you correctly clean up everything you do in use effect or use layout effect, you're you're that is your unmount. Like there's it cleans up. Like unmounting, there's two things for it. One is cleaning up, and two is like, I don't know, you need to actually do some interaction with the user or something when your component gets unmounted. And I would say for that case, you're probably doing things wrong. Like you should probably be listening to a JavaScript event or something like that, you know, like not waiting for your component to be unmounted. That's like a passive signal. You should be looking for an active one. Um, and the other thing is, yeah, so if you need to do cleanup, that happens in the function that you return from use effect or use layout effect or any of the things in that family. If you use those properly, if you always clean up your effect that way, you should never need an on unmount hook, in my opinion. I don't know, does that make sense, Austin?

SPEAKER_02

Yeah, what what you're saying about cleaning up makes sense to me. Like within each uh effect, there should be a return with your cleanup stuff in it. Yeah. Um, but then I think about like, okay, what about these like floating state values that are in the component, etc.? Like the other day, that this happened with the React Hook form. Um there, there's like we have a controlled form component that I fill out the data for this form, and then like on submitting it, I need to make sure that I reset the defaults of the form because like I submitted it and then I navigated back, and then I navigated forward again back to the form. And because I hadn't reset the values, somehow they were still persisted in the form. And I was like, wait, what? So like these kind of things, it's not tied to an effect, but it is still something that needs to be cleaned up.

SPEAKER_01

So this gets back to what I said initially is I don't think that's the right place to clean it up. Shouldn't you clean it up after the like the form gets submitted? You send that request to the server, and then in that same body of code, isn't that where you should clean it up?

SPEAKER_02

Exactly. That's where I ended up implementing it. So is that like, is that going to so that that seems to be like, okay, yeah, after this action, I should run like a cleanup uh within this function, not within an effect. Um what uh what about so uh on that action? I would just have to look at everything that's within the form and say, okay, I need to clean up all of this stuff within this function after it like on wrapping up execution. Is that the proper place to do like all of these things?

SPEAKER_01

Or okay, yeah, I I think like and maybe this is why I don't run into these issues. That's natural for me. I'm just like, hey, I've done something with this data. I now need this this data or this UI or whatever to be in the next state for whatever somebody does next. Like immediately. I'm not like, let me wait for something to change in the background to do this. I'm like, no, I I just just do it, you know? And that's making me think too about the like use once thing. Like, you know, you said like this is the most common case, it's 80% of what people do, right? Like they they want to do it um when when things are being mounted, and it made me think that I don't think that's right either. I think and I think actually Facebook um I think they're like when we did that uh episode on React 18, um, I think it touched on some of that, and that um the order, like the thing that triggers you know these network requests and stuff, it shouldn't be tied to when our component gets mounted, right? Like uh it should be tied to the initial action. So like let's say this. Let's say we have a form or we have a page and you have a button. And when the user clicks this button, it brings up a window or a screen or whatever, and it has a form on it, you know, to create some new entity, right? A very very common thing, a crud interface, right? So um let's say uh we needed some data for that form, like maybe for a type ahead or something, right? So a lot of people, they're like, okay, inside my form, when this mounts, I'm gonna fire off the network request to get this data. The right place to do that would actually be when the user clicks the button to bring up the form. Because that's the point at which we know we need the data. If you wait until your component is mounted, you're actually waiting longer than you need to. And so that's where I think of things as like our interactions in this way should be directly tied to the first place in which they're triggered. And what I find in a lot of React code is people try to tie everything to effects. They're like, oh, anytime I have a network request, it needs to be in a use effect on my component. Anytime I, you know, oh, and this is something I see a lot too, is like people will like um listen for the state change. Like, like, say you have a use state in your component, they will have a use effect with a dependency array referencing that value. And I'm like, why would you ever need that? Like, if something changes that value, it's inside your component. You should be updating doing whatever code is inside that effect right away. Like, why are you waiting? Why are you running this later? Like, there's I I don't know of any reason to do it, but I think that's the maybe that's the sort of twist on all of this is like twisting your brain to think about it in a in in doing these things right away instead of delaying, instead of always trying to stuff it into to hook to like effects, basically. I don't know. Does that make sense, Austin?

SPEAKER_02

Yeah, it does. I think this is uh this is kind of tying back to I I was making this mistake where I was like setting some filtered value via state, and then I was listening to that on a use effect to run like the side effect of like that state change, and then I just put it all in this one function, it was like, oh, this is doing now what I wanted without that extra re-render, and I I think I'd gotten into this like use effect all the things paradigm, right?

SPEAKER_01

Yeah, and I I think that I don't I'm wondering where that comes from. Is it coming from like the class-based lifecycle way of thinking, or is it just the natural human way of thinking? But it's very common. Like I see this with basically every piece of React code I've ever seen, to be honest.

SPEAKER_02

I think it's just um the functional programming style craze a few years ago that like got into JavaScript and everyone was like doing all these, like everything functional for a while.

SPEAKER_01

Well, it is functional to do it this way too.

SPEAKER_02

No, I know. I I'm and I think that led to like oh React just like release functional components, and there's this thing called use effect now that we can do, and so it was just like that became the hammer for all the nails. And yeah, yeah, it just normalized that way.

SPEAKER_01

Yeah, yeah. So uh everything uh is is uh a nail when you have a hammer, I guess is is the conclusion here. Um, but maybe, yeah, maybe we need another episode or something on you know, sort of reframing the way we think about these things. Um I don't know if that's helpful or not, but I guess we could consider it. Um yeah, and I think uh, you know, you you really touched, I think we both touched on the updating state and and where to do that. Um yeah, it's kind of funny. Like I think in class-based components, it was kind of more clear like where to stuff everything. Um you you'd stuff everything into you know um component did mount. Um, and and that was more clear. But the thing that I always hated about it, and I know the thing that React team hated about it, is then you had to like combine um unrelated things. Like all of your component did mount stuff had to go together in that one place, and then you had to remember to clean it all up on component did unmount or whatever, right? Like, and it was all bunched together. I hated that. I was like, I would always forget to clean stuff up. Um, so I definitely appreciate that that is not as much of a problem anymore. Uh, at least they're tied together. You can look at your use effect and be like, oh, this needs cleanup and just return the cleanup, you know. Um, I I definitely appreciate that. Um, yeah, so I think I don't know. I think to me that covers the sort of life cycle methods thing. I I know there's more we could talk about, you know. Um like Austin said, how do we replicate all of these other ones that already existed? And I guess if that's what you all are interested in, we could certainly talk about it more. Um I think we covered the the main one, uh the on mount. Um, so so hopefully that's good. On on mount, we covered that. Um, but if there's other things, yeah, I guess let us know. Or I don't know, Austin, did you have any other ones you wanted to talk about? Or does that cover it for you?

SPEAKER_02

Well, I mean, there's these like component will update, will unmount. Um, I don't I don't know if I ever really found myself needing to use these. I feel like they're kind of edge casey. Um, but I feel like these can also be mitigated, like we just talked about before like instead of using life cycle hooks, just run that execution within the function uh and you should be fine. I don't think that there are any more that really need to be looked at.

SPEAKER_01

Okay, yeah, that was that was kind of how I was looking at it. Um so yeah, sounds good. Um, you know, uh I also wanted to just touch on like sort of moving to a different topic here. Uh you know, knowing how React is like doing the bookkeeping and these life cycle, the life cycle of the component, um, should that influence the way that we divide up our components? Like should you uh create different components based on these types of life cycle things? You know, like one component might you're like, oh, I want this one to be in the DOM all the time, but this one might come and go. Does that mean you should set up your components to be structured in that way? Um I I I didn't tell you ahead of time about this, Austin, but I'm curious if you think that should matter or not.

SPEAKER_02

I feel like there is a reason that I feel like it should matter. Like we we should at least like every everything needs to be understood, like why certain things are happening a certain way. And I yeah, I think that the like we've gotten to this point because it hasn't really mattered and we're now having to discuss it, so it does matter, you know.

SPEAKER_01

Yeah. Yeah, I think in terms of like where we you know run our code, like we've just been talking and ranting about, I think it matters. But I would actually say you should never pay attention to this when it comes to like your components themselves, uh, unless you've proven that there's an issue. Generally, if you had a performance bottleneck where solving it, you know, could be solved by splitting up your components or something. Um, I think that makes sense, but I don't know. In my mind, abstractions are for the human, not for the computer.

SPEAKER_02

So yeah, that's exactly the the main thing. I I think it's just about like how how is your team working? How can you make it more readable for like other people? How can you make it more readable for yourself when you come back to it? Uh I know that like I did this one um doing discount codes for uh this like huge form that I had created and it had it had something like eight different specific components I had to create to handle certain things and the file was like maybe like 2,000 lines long at the end of it. And I it was requested of me to like split them up into different files to like be able to read it better, but now when I go back to it, I'm having a hard time reading it because I'm like jumping around between all these different files, and I'm just like, which way is the right way? I don't know.

SPEAKER_01

Yeah, that's like the classic um best practices type thing, right? Like your file should never be longer than this, no matter what. If it is randomly chop it up until it fits.

SPEAKER_02

Well, and this goes back to like code review culture, which we've talked about as well, is like focusing, like, are we focusing on the code or are we nitpicking like uh like hey, shouldn't we this the string like we should uh define everything in constants? It's like okay, yes.

SPEAKER_01

Yeah, you know, I was actually just thinking about that um on that code base, I don't know, a few days ago, and I was like, you know what? Like we have more maybe minor issues like that, but I don't know, it like working with it just feels way better. Like I feel less like I'm gonna break stuff and I change something. I don't know if you feel that way, but like it definitely seems to have made an impact.

SPEAKER_02

I I feel like I have a little bit more freedom to just write code and not worry about it so much.

SPEAKER_01

Um and does that feel like it empowers you to write the code that ends up being better?

SPEAKER_02

Yeah, I feel like these um these like standards and best practices they they may be good for like like a lot maybe go back and refactor after you've like, okay, hey, like we're pretty sure about this implementation. It's looking good. I I'm pretty solid about it. Okay, this let's now abstract out this stuff, but I feel like when I'm having to remind myself to do that while I'm in the creative process of figuring out the problem and creating the solution, if I'm doing it all based around these best practices, I find that I'll get lost. Like the flow gets interrupted really rapidly. Um, or I'll like I'll be like, yeah, I'll just be writing it. Um I use like the double exclamation mark as like to do a null check on something, and it's like no, like we use Boolean uh and like this, and like I go to I'm like, oh, okay, and so I like rewrite that, and then I'm like, wait, where what was I trying to do now that I like had that interruption in my brain about the minor syntax like choice? So yeah, I feel I feel a lot better now, and I think it also maybe sure, maybe they aren't like always best practices, um, they're just standard practices, right? And um maybe the best practice is to just write it how you see fit in that situation, and maybe you end up discovering a new best practice out of that, or it brings up some questions of like, hey, uh, you know, like why are you doing it that way? Or like, here's a way that I found we could do it differently. But if you're just stuck in these, um if you're stuck in your box, you lose a lot of dough on cookie cutters, you know. Sometimes you can make another cookie out of it, but it's usually like it looks weird because it's very clear that it was like put together with all the other chopped off like arms of the dough and yeah um the flowers already mixed, and then you end up throwing that stuff away, and it's like, I don't know. I'm not convinced.

SPEAKER_01

Yeah. Well, you know, the way that I think of it is like, um, think about the best practices that you know. Are they ever like dealing with the abstraction itself, like trying to make this code abstracted well? Or are they always these sort of simple, easily measurable like things, like the the double, you know, negative for the Boolean or whatever? Like, like that's what it seems like to me. They're always these like, I don't know, kind of cheap, easy things that are like that somebody on their high horse can just be, you know, uh but go down the list and point out all these like minor things you did wrong, you know. Like do they actually make the code significantly better though?

SPEAKER_02

No, and like we mentioned before in the code review thing, you miss the you miss the forest for the trees for the whole thing. You uh somehow the person that is now like fixing up the code, like is maybe even like feels invalidated about like the whole feature they wrote because it was just like filled with these little like paper cuts that you someone just like all over in you, and you're just like I just like spent a week doing this, and now this is all you really have to say.

SPEAKER_00

Yeah, oh totally, yeah, yeah.

SPEAKER_02

And I I mean I think that these best practices too, they can really they're they're abstraction choices that you get tied into, and then it's like you look at the code base later and you realize maybe like, oh, we maybe shouldn't have like enforced that because now we have this whole paradigm that we're like we're on these railroad tracks following, and it's like is it is this train track about to go off the cliff or not? Like, are we stuck just heading in that direction, or can we can we uh change? And then that leads to a one to five hour meeting with your team going over this mundane dumb stuff where you're just like so yeah, those are my opinion on it because I I mean not not at all trying to rant, but I do think it's very important to like consider like this whole bottom-up approach, just like get it working, determine afterwards if you really need to abstract it and be very careful about actually doing that because we know software programs are they're never done, right? So if you like abstract out things and you're like, all right, it's done, you're lying to yourself, and um you may go back in and you're like, oh man, I like packaged that whole box together perfectly. Now I gotta unpack it again, and it's like it can make future development difficult. So that's something I just I guess I had to say.

SPEAKER_01

Yeah, I totally agree. And actually, so um, I'm I'm gonna continue this tangent a little bit. Hope you hope you all don't mind. Um, so I actually maybe like 10 or 15 years ago, I wrote a really popular uh article on on my personal site, and it was programming best practices, and I was so proud of it. And I published it and everyone was just like eating it up, you know. I think I took it down like four days later because I felt so bad about it because like it made me realize like like everyone's giving me feedback, like, oh yeah, totally. You should never put a semicolon here or whatever, you know. Like, just and then like within a few days, I was like, uh what? I this is just not who cares, you know. Like I just 404 it. I was like, I know I'm getting a lot of traffic, but I just feel too bad. It feels too I felt gross inside.

SPEAKER_02

Well, it it feels like it's kind of the topic, it's almost like it's this easy thing to jump in on and like like absolutely it's like clickbait almost.

SPEAKER_01

It's like it is clickbait, yeah. It's programmer clickbait. You're totally right. That's what I'm gonna start calling it. I love it. You coined it.

SPEAKER_02

Yeah, no, you coined it.

SPEAKER_00

It's a programmer clickbait.

SPEAKER_01

It's just yeah, anyways, you know, sort of relating this back, you know. Um, maybe we didn't give you all the exact answers and standard best practices for you know re-implementing use on mount or whatever. Um, and we talked a lot about how you need to reframe your way of thinking. And I know that's not clickbait-y, but that's what you get with us.

SPEAKER_02

Welcome to the react show.

SPEAKER_00

Welcome to the react show where we don't clickbait usually.

SPEAKER_02

I was I was trying to come up with a clickbait like thing to for our outro.

SPEAKER_00

Yeah, I think we'll move towards that.

SPEAKER_01

Um uh yeah, so uh we took a look at uh such wonderful things as how React thinks uh of a component and the bookkeeping. And we looked at the lifecycle, mounting, updating, post-updating slash effects, and unmounting. Talked a bit about like the sort of scoping of components. Um and and then we talked about you know the replicating some of these lifecycle methods with hooks, you know, how to do it, when you should do it, if you should do it, all that good stuff. Um, and you know, as you just heard, a little tangent on best practices. Um, but yeah, that's our uh I think that's our show for today. I hope you all uh had a good time. I know Austin and I did. Um and you know, like always, if you have other thoughts on on hooks, um, on the component lifecycle, that kind of thing, or just in general, um, you know, uh let us know, uh feedback or questions. You can send them to the React Show at owlcreek.studio. Um, yeah, Austin, did you uh want to say anything uh to to send our your our listeners off here today?

SPEAKER_02

Yeah, please like reach out with any of these QA topics. Like we we would love to, even if there you think the there is no such thing as a dumb question, and we would love to answer anything that you have in mind, even if it's like a really small beginner's thing or it's a larger topic. We're here and we're open to discussion.

SPEAKER_01

Absolutely. Uh totally agree, and and thanks again uh for joining us. We hope you have a great rest of your day, your week, uh, your holidays, whatever it might be. Uh see you later.