JedWatson / JedWatson/react-select

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

Open
#4,973 7 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

awaiting-author-response issue/bug-unconfirmed
Dominant language
TypeScript
Stars
28k
Forks
4.1k
PR merge metrics
No merged PRs in 30d

Description

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 )

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the Select example and the linked CodeSandbox, then trace how Select and AsyncSelect resolve value and defaultValue when getOptionValue is supplied. Done means an option is selected from its id string in both the regular and asynchronous cases, with coverage for the reported behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.