So the first thing I think is conceptually the cause of this issue is thinking that functional components are actually a thing that exists and I think that is kind of a mental trap that we should probably dive into a little bit deeper. So let's look at that. When we have a React class component which is how most of us started out doing React, you have React creating a new component. So then it has an instance of that component that it keeps and it runs component did mount, component will receive props, component did update and all of that stuff and finally render. It will continue running that entire life cycle and producing JSX on that instance that actually exists that it has for the lifetime of your component until it's unmounted.
Now let's look at a functional component. The first thing that React does is it takes the props and it passes those props into a function and this function is what we think of as your component and then that component will produce JSX. If the JSX is the same as the JSX from the previous time that it was called, that it called that function, it will re-render it otherwise it doesn't do anything with the result of calling that function. So but at any rate, as soon as it has that JSX, that render of your function is done, you're out of that world. And so yes, it will call your component function again, but it's like an entirely different new thing that is happening.
So let's go a little bit deeper into how creation and destruction and things works in JavaScript so we can really get a handle on what's going on here. So let's start out with a simple case where we create a function that logs a date. So if we have this log date function and every time we run it, it creates a new date, then if you run this function 99 times, you will create a new date. I'm sorry, if you run it 100 times, you will create a new date every one of those 100 times. Now let's look at this a little closer. Let's say that we have a function that's designed to create a new date and we store the result of that function in a variable and we're going to also run it the other 99 times and we're just going to run it. We're just not going to store the date. So we still created 100 dates, but we only keep one of them and we'll talk about what happens to the rest of them later, but in essence the only one that we can do anything with is the one that we stored in the variable. The rest of them just go away.
So now let's look at, we do the same thing, but then we set my date to undefined. So in this case, we created 100 dates and we don't reference any of those dates. So they were created, but they are actually no longer serving any purpose in our program.
Now let's go a little bit further because React now is very skewed towards the functional side. We're going to create a function that creates the date logger that we just looked at and returns the function. So if we run date logger creator 100 times, whether we keep a reference to it or not, we don't create any dates. We create the function, but we don't create any dates. So by running this function, we actually create 100 versions of this function, but no new dates. Now let's change it a little bit again. Now we're going to create our date logger creator so that it creates a new date, and then that is the date that will always be logged by the date logger. So what's going to happen here is we create the date logger 2, and it will create a new date, but every time you run the my date logger, it's going to only log this date.
Comments