epicweb-dev / react-hooks

Learn React Hooks! 🎣 ⚛
https://hooks.epicreact.dev
Other
2.78k stars 1.91k forks source link

I guess using index as item key is considered a very bad and dangerous practice #152

Closed golestanirad closed 3 years ago

golestanirad commented 3 years ago

Hi there 👋 In here https://epicreact.dev/modules/react-hooks/usestate-tic-tac-toe-extra-credit-solution-3 you used the index in the map loop as the key for each li which I guess is considered a very bad and dangerous practice, could you tell us why did you go that way? I'm 100% sure that you are aware of that issue so thought might be something new in React that I'm not aware of.

We don’t recommend using indexes for keys if the order of items may change ... https://reactjs.org/docs/lists-and-keys.html

kentcdodds commented 3 years ago

It's only bad practice if the order of the items can change, in our case the order of the items never changes so it's fine.

If you have further follow-up questions you can bring it to office hours

golestanirad commented 3 years ago

It's only bad practice if the order of the items can change, in our case the order of the items never changes so it's fine.

If you have further follow-up questions you can bring it to office hours

Thanks, good to know! But it was nice if it was mentioned in the tutorial, as someone like me may don't know that, and then may start using the index as the key! because we are students and learn things from you!

kentcdodds commented 3 years ago

Good point!