question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Option of AutoComplete Select should not use value as a key, as value may not be unique and React requires keys to be unique

See original GitHub issue
  • I have searched the issues of this repository and believe that this is not a duplicate.

Version

3.8.4

Environment

Chromium 68.0.3440.75, Firefox ESR 52.9.0

Reproduction link

Edit on CodeSandbox

Steps to reproduce

Use Select component and pass key and value properties to its Select.Options. Make sure that there are two same value values. Notice warning about duplicate keys in the browser’s console.

What is expected?

key property in Select.Option component is used as component key.

What is actually happening?

value property is being used as component key instead.


One can notice the bug while looking at https://github.com/ant-design/ant-design/blob/3.8.4/components/select/index.tsx, the Select.Option and Select.OptGroup components does not support key property as one could expect after reading documentation.

export interface OptionProps {
  disabled?: boolean;
  value?: string | number;
  title?: string;
  children?: React.ReactNode;
}

export interface OptGroupProps {
  label?: React.ReactNode;
}

That issue was raised before in #11074.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

17reactions
afc163commented, Aug 27, 2018

I don’t know what component you should use but probably you need Select[showSearch], you can checkout it. But still, value of AutoComplete should not be duplicated.

5reactions
syberencommented, Aug 23, 2019

This still appears to be a problem. I can set a key prop on <AutoComplete.Option>, however as soon as I set a value prop this is used as the key.

I understand that value should not be duplicated, but in some cases this is not avoidable, for example when using a Belgian zip code API, where multiple towns can have the same zip code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I add unique keys to React/MUI Autocomplete ...
You can define your own renderOption that can return the list item with a correct key value. Your code complains about the duplicated...
Read more >
React Autocomplete component - Material UI - MUI
The autocomplete is a normal text input enhanced by a panel of suggested options. The widget is useful for setting the value of...
Read more >
A Guide on Material UI AutoComplete in React - Refine Dev
Material UI provides a unique set of components and utilities to help ... The component's value is obtained from a predetermined range of ......
Read more >
Warning: Each Child in a List Should Have a Unique 'key' Prop
If the key is an index, reordering an item in the array changes it. Then React will get confused and re-render the incorrect...
Read more >
Combobox (Autocomplete) - Headless UI
Each component exposes information about its current state via render props that you can use to conditionally apply different styles or render different...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found