React-select: multi value select uses getOptionValue for react key, doesn't work for non-prim values.

Created on 30 Dec 2018  路  6Comments  路  Source: JedWatson/react-select

I use non-primitive values for my options (providing a getOptionLabel to turn them into strings.) The problem is https://github.com/JedWatson/react-select/blob/916ef8f2813dbdd7bb5adb186ab3a085d40003c3/src/Select.js#L1478

uses the value for the react key. When isMulti={true}, I see a lot of:

image

Most helpful comment

The good way to work with complex object value is to use getOptionValue()
and getOptionLabel(). It's not very clear but works very well, as we had exactly the same problem.

const CustomSelect = (props) => {
  return (
    <Select
     getOptionValue=(option)=>option.user.id
     getLabelValue=(option)=>option.user.fullName
      {...props}
    />
  )
}


All 6 comments

maybe a getOptionKey prop would be appropriate?

@toshok You could replace the <MultiValue /> component with a custom component and declare the key however you like:

import React from 'react';
import Select, { components } from 'react-select';
import uuidv4 from 'uuid/v4'; // Generate unique id

const MultiValue = (props) => {
    return <components.MultiValue {...props} key={uuidv4()} />;
};

const CustomSelect = (props) => {
  return (
    <Select
      components={{ MultiValue }}
      {...props}
    />
  )
}

Nvm, this doesn't work because the custom component still gets wrapped for some reason :confused:

Screenshot from 2019-04-30 18-04-18

The good way to work with complex object value is to use getOptionValue()
and getOptionLabel(). It's not very clear but works very well, as we had exactly the same problem.

const CustomSelect = (props) => {
  return (
    <Select
     getOptionValue=(option)=>option.user.id
     getLabelValue=(option)=>option.user.fullName
      {...props}
    />
  )
}


Hello -

Thanks for your assistance @nicolas-zozol.

In an effort to sustain the react-select project going forward, we're closing issues that appear to have been resolved via community comments or issues that are now redundant based on their age.

However, if you feel this issue is still relevant and you'd like us to review it - please leave a comment and we'll do our best to get back to you, and we'll re-open it if necessary.

image
MultiValue component created by Select is throwing "Warning: Each child in a list should have a unique "key" prop." after selecting first value from Select with isMulti set to true.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pablote picture pablote  路  3Comments

pgoldweic picture pgoldweic  路  3Comments

mjuopperi picture mjuopperi  路  3Comments

juliensnz picture juliensnz  路  3Comments

AchinthaReemal picture AchinthaReemal  路  3Comments