2 ms·
> we just use the index from the .map() loop to add a key to every element Doing this could hide weird bugs. Let's say we render a list of posts with "remove"
by ash 11y ago
> we just use the index from the .map() loop to add a key to every element
Doing this could hide weird bugs. Let's say we render a list of posts with "remove" button for each one. The user clicks on "remove" button, we modify the underlying this.state.posts. And on next re-render removed post key would be assigned to the next post! The next post could inherit just-deleted post handlers, for example.
It's much better to use unique keys like database ids.
- dominic_cocch 11y agoGreat point. This has to be the funniest and most confusing bug when you first encounter it! If your list is totally static, an index will do the job. However, we usually end up appending something like an id from the db since almost nothing on Instacart is static.