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.

Nothing is displayed when value is programatically set and getOptionValue is used

See original GitHub issue

When getOptionValue is used, value or defaultValue still require using full object and don’t respect the getOptionValue result.

const allItems: Item[] = [
  { id: 1, name: "Tesla" },
  { id: 2, name: "BMW" },
  { id: 3, name: "Audi" },
  { id: 4, name: "Mazda" }
];

  const getLabel = (item: Item) => item.name;
  const getValue = (item: Item) => item.id.toString();

And when I use

        <Select
          getOptionLabel={getLabel}
          getOptionValue={getValue}
          options={allItems}
          defaultValue={allItems[0].id.toString()}
        />

it doesn’t work and doesn’t select the proper value. Neither does using value property. But if I use defaultValue={allItems[0]} it works - so it seems it doesn’t respect the usage of getOptionValue.

Normally, I would use something like allItems.find(item => item.id.toString() === selectedId), but that is not possible to use in AsyncSelect, since I don’t have access to loaded options.

(I’m trying to use react-select with react-hook-forms and I have noticed these issues while working on this integration. Here is more detailed example: https://codesandbox.io/s/admiring-monad-tq2er?file=/src/App.tsx )

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:5

github_iconTop GitHub Comments

3reactions
klinkicommented, Jan 11, 2022

Hello @klinki,

the values for the value and defaultValue props should have the same schema as a single option in your list of options, as both function essentially independently from the options prop. In other words: The value does not have to be in the option list.

Well, I’m not so sure about this one. Shouldn’t they have the same schema as output of getOptionValue function call?

Honestly, I don’t care that much if the value is or is not in the list (my example with array was meant as a workaround for this issue, not the solution). I just need it to respect the getOptionValue function, because I want to use react-select in the form and integrate it with react-hook-forms.

2reactions
klinkicommented, Jan 19, 2022

That’s nice, but impossible to use with AsyncSelect

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-select change name of option fields from ({ value: '', label ...
Hi, I'm currently using the CreatableSelect from react-select . I have the same issue with the labels, if I use your example for...
Read more >
API - React Select
getNewOptionData. Returns the data for the new option when it is created. Used to display the value, and is passed to onChange ....
Read more >
What is the point of getOptionValue prop ? #3388 - GitHub
So using { id: 'one' } as the value does not show anything as well as a simple string. Both fail silently because...
Read more >
get_option() | Function - WordPress Developer Resources
Retrieves an option value based on an option name. Contents. Description; Parameters; Return; More Information; Source; Hooks; Related. Uses; Used By.
Read more >
Add, select, or clear items | Select2 - The jQuery replacement ...
Programmatically adding, selecting, and clearing options in a Select2 control. ... Set the value, creating a new option if necessary if ...
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