Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material UI autocomplete duplicate key warning

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.

  1. Warning when passing an object as a dataSource item. I get a duplicate key warning: "Warning: flattenChildren(...): Encountered two children with the same key"

    {
      contact,
      key: index,
      text: FullName,
      value: &ltMenuItem key={index} primaryText={item} /&gt
    }
  1. When typing the rendering is slow, because sometimes it matches a lot of contacts. Ideally I would like to show maximum 5-10 contacts in the autocomplete, but that is not possible yet. (that feature seems to be accepted in a PR already, or?)

Thanks,

like image 959
Liviu Ignat Avatar asked Jul 25 '26 10:07

Liviu Ignat


2 Answers

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>
              );
            }}
like image 168
Safi Habhab Avatar answered Jul 28 '26 13:07

Safi Habhab


Autocomplete now supports the getOptionKey param.

like image 24
Cristiano Mendonça Avatar answered Jul 28 '26 13:07

Cristiano Mendonça



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!