Current section: Rendering Arrays 59 exercises
solution

Key Reset

Loading solution

Transcript

00:00 So, all we need to do is add the key prop here, key is key, and you'll notice if you're familiar with how a use state works and everything, then this shouldn't be too complicated for you, but just as a quick intro to the uninitiated, this use state is initializing this value

00:17 as zero, and so we get that value of zero, and we're setting zero as the key for the input. And then when this button is clicked, we're updating that key to be whatever it currently is, plus one, so it could be one, two, three, four, and it continues every time you hit the reset button, which means that the key prop here is going to change, and so React

00:35 will say, oh, okay, I'll remove that element, that input, and we'll put a new one in, it's not gonna have any of the same state or anything, and that's why this ends up working. So I say, oh, I gotta save it first, and then I say, hello, and when I hit reset, then we're gonna remove that input and put a new one in with all of that state gone.

00:55 So there you go, that is using the key prop to reset an element, you can put this on an entire chunk of your UI, however you want to do that, and that can reset that entire chunk of UI, again, not something you do a ton, but absolutely something you do sometimes,

01:15 so just put this in the back of your mind to think on in the future as a mechanism for resetting the state of, or your UI for a particular area, because it can be useful sometimes. Great job!