Current section: Rendering Arrays 59 exercises
solution

Key prop

Loading solution

Transcript

00:00 Okay, so this is actually super easy and like I said a linting plug-in is going to tell you when you got something wrong So you should be just fine, but here we're going to say item Dot ID so we're mapping over those items or just pass ID

00:16 The ID of the item for the Li and so that way when that item is no longer in the list then react knows Oh, they actually removed that a lot. Let's delete that that Li will make a new one or whatever So here we're removing the Apple the orange stays with the orange because those things are associated so it knows what to do

00:34 And we can add the item back and then remove remove remove and all of that if you take a look at the dev tools It's kind of interesting how all this Like come comes out in the way that it works. So here we've got our Li We hit remove and it's it's just removed. The other ones are not bothered. Everything works out

00:53 Just fine if we remove the key prop though, and we refresh and take a look at this Now if I remove this, you notice this Li stayed put and it just updated the ID updated the for updated the content It actually incidentally it also updated the value. But again, that's an attribute thing. It's not a

01:13 Actual technical value so react is not going to update that value for us because it says oh no that was just the input that we had before so I'm just going to update all the attributes and everything but yeah that the The same input that was there before is still there and the text content is

01:33 Just going to be the thing that's changed. So by providing a key prop We're giving react much or as much information as it needs to be able to track the elements Associated to the data and we interact with the data then the elements can follow along