react-select icon indicating copy to clipboard operation
react-select copied to clipboard

[v2] Warning: Each child in an array or iterator should have a unique "key" prop.

Open petrdanecek opened this issue 8 years ago • 10 comments

version: "react-select": "^2.0.0-beta.6" (I recently migrated from version 1.2.1, works fine in v1)

issue: FF reports: [v2] Warning: Each child in an array or iterator should have a unique "key" prop.

subsequent issue: I'm not able to select second value in isMulti mode, most likely due to above error. Selecting first value raises above error, selecting second option cause the blank value is set (the selection is reset to empty)

The code snippet (heavily simplified, the options hardcoded into callback instead of fetching from API). The ID is used as search value for API fetch.

class ProjectSelect extends React.Component {
  constructor(props) {
    super(props);
    this.loadOptions = this.loadOptions.bind(this);
  }

  loadOptions(inputValue, callback) {
    callback([
      {
        projectId: '123',
        id: '1',
        description: 'Project A',
      },{
        projectId:	'456',
        id: '2',
        description: 'Project B'
      },
    ]);
  }

  render() {
    const { isLocked } = this.props;

    return (
      <AsyncSelect
        name="Selector"
        cacheOptions={false}
        loadOptions={this.loadOptions}
        getOptionLabel={opt => opt.description}
        getOptionValue={opt => opt.projectID}
        isDisabled={isLocked}
        isMulti
        hideSelectedOptions={false}
      />
    );
  }
}

petrdanecek avatar Jun 22 '18 20:06 petrdanecek

I just had the same problem and looking through the source code and examples it seems like you need your data formatted as follows:

[{ value: 'ocean', label: 'Ocean' }]

The value is used as the key for each MultiValue item so it needs to be unique. The label is required too.

What might work would be:

loadOptions(inputValue, callback) {
    callback([
      {
        value: '1',
        label: 'Project A',
      },{
        value: '2',
        label: 'Project B'
      },
    ]);
 }

jc211 avatar Jun 23 '18 12:06 jc211

I too am having this issue (using Chrome and also on version 2.0.0-beta.6). As per @jc211's comment, I'm not sure that is really the root cause of the issue. All the items in my options array are structured as objects with label and value strings (which I have verified to be unique across all options). I am using the basic (i.e., non-Async) Select component.

What's interesting is that each of the individual Select instances I'm using works fine as a standalone unit. The key error arises when I select an option from one specific Select instance on the page, at which point the other two Select instances immediately break.

IsenrichO avatar Jun 24 '18 06:06 IsenrichO

Well I'm having lot of my components depended on reactSelect v1. v1 has the key/value optional and expect this in the configuration:

labelKey | string | 'label' | the option property to use for the label valueKey string | 'value' | the option property to use for the value

So in v1 I just used this:

<Select
       labelKey="projectId"
       valueKey="description"

now in v2 above is deprecated and been replace by function..so I changed the config to:


<AsyncSelect
        getOptionLabel={opt => opt.description}
        getOptionValue={opt => opt.projectID} 

I'm going to take a look to source code. I believe it is just the problem in the key used in some .map method.

petrdanecek avatar Jun 24 '18 08:06 petrdanecek

So in src of Select here is the key of multivalue map:

if (isMulti) {
      const selectValues: Array<any> = selectValue.map(opt => {
        let isFocused = opt === focusedValue;
        return (
          <MultiValue
            {...commonProps}
            components={{
              Container: MultiValueContainer,
              Label: MultiValueLabel,
              Remove: MultiValueRemove,
            }}
            isFocused={isFocused}
            isDisabled={isDisabled}
            key={this.getOptionValue(opt)}

getOptionValue is:

getOptionValue(data: OptionType): string {
    return this.props.getOptionValue(data);
  }

seems there is no issue....don't understand...I will add the error screenshot and check it again tomorrow.

petrdanecek avatar Jun 24 '18 09:06 petrdanecek

Nevermind... you can ignore my comment for now. It seems part of the issue is coming from our Redux store doing unexpected things.

IsenrichO avatar Jun 24 '18 20:06 IsenrichO

The error from FF console: image

petrdanecek avatar Jun 25 '18 07:06 petrdanecek

I can confirm that once I change the options to label/value keys, the issue is gone and even multivalue select works like a charm... So the options as per jc211 works well..

petrdanecek avatar Jun 25 '18 07:06 petrdanecek

I found the root cause...it is mismatch in the value key name...the getOptionValue={opt => opt.projectID} is supposed to have small D in the ID to respect the options projectId. so the fix is: getOptionValue={opt => opt.projectId}

petrdanecek avatar Jun 25 '18 07:06 petrdanecek

I'd just like to add if someone in the future comes here and doesn't find these comments helpful. I had this exact 'warning' but my problem was that somewhere in the code, the options were being remapped to include just the value string.

So options were going from [{value: 'something', label: 'something'}] to ['something'].

sahibjotsaggu avatar Sep 07 '18 18:09 sahibjotsaggu

if you are not going to use [{value: 'something', label: 'something'}]

you must specify exactly id and name data=[{_id: "5e2715b2171b6045d46ce647", name: "Laboratorio de alimentos", description: "Experto en el tema de Laboratorio de alimentos", updated_at: "2020-01-21 10:16:02", created_at: "2020-01-21 10:16:02"},{_id: "5e2715b2171b6045d46ce648", name: "Laboratorio de organicos", description: "Experto en el tema de Laboratorio de alimentos", updated_at: "2020-01-21 10:16:02", created_at: "2020-01-21 10:16:02"}] <Select isMulti hideSelectedOptions={false} name='teamIdsFilter' menuPosition='fixed' options={data && data} getOptionLabel={option => ${option.name}} getOptionValue={option => ${option._id}`} placeholder='select' onChange={onSelect}

                />`

` const onSelect = (optionsList) => {

setNewTemplate({ ...newTemplate, specialities: optionsList });

};`

CrisGarcis avatar Jan 22 '20 13:01 CrisGarcis