downshift-js / downshift

🏎 A set of primitives to build simple, flexible, WAI-ARIA compliant React autocomplete, combobox or select dropdown components.

Home Page:http://downshift-js.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Usage of array index in the key props

NathanVss opened this issue · comments

Hello, it's not a bug report but I was wondering why the examples include the usage of array index inside options key prop ? Like here https://www.downshift-js.com/use-select#basic-usage


             <li
                className={cx(
                  highlightedIndex === index && 'bg-blue-300',
                  selectedItem === item && 'font-bold',
                  'py-2 px-3 shadow-sm flex flex-col',
                )}
                key={`${item.value}${index}`} <--- Here
                {...getItemProps({item, index})}
              >

It is usually seen as a bad practice but I think that you did it on purpose, so I was wondering why 😊

Thanks!

It's considered bad practice since that's what React is actually doing by default, if you don't pass a key prop.

But we are not only using the key, we are also using item.value. Just in case there are more items with the same value (unlikely in our examples). Ideally we should have gone with item.id, where id should be unique.

Actually I just found the actual problem with our code.