I am using Material UI through a project and I am experiencing some small issues with the AutoComplete component when trying to load a bigger list of contacts, some contacts may have the same name (because of test data), but different ids.
{
contact,
key: index,
text: FullName,
value: <MenuItem key={index} primaryText={item} />
}
Thanks,
some times you have records in mui autocomplete that has the same display property, for example. in a list of users, 2 uers may have the same name. mui auto generates a key based on that property so you get duplicate keys.
the solution is to customize the key. and to do that you need to customize the rendering of the autocomplete list.
use renderOption property
renderOption={(props, option, index) => {
const key = `listItem-${index}-${option.id}`;
return (
<li {...props} key={key}>
{option[optionProperty]}
</li>
);
}}
Autocomplete now supports the getOptionKey param.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With