JedWatson / JedWatson/react-select

react-select looses focused item if options are change on render

Open
#5,236 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
28k
Forks
4.1k
PR merge metrics
No merged PRs in 30d

Description

Hello,

I bumped into the issue about focusing selected item. It doesn't work in my scenario because items in options can be recalculated from time to time so they loose reference to them.

As I can see react-select uses indexOf(selectValue[0]) a lot to determine next focus index. So it returns -1 in my case because reference to options changes.

Shouldn't it look for options using values instead? This way it will keep working even after reference was changed, e.g it could look like:

const selectedIndex = focusableOptions.findIndex((opt) => opt.value === selectValue[0].value);

What do you think?

Here you can find reproducible example https://codesandbox.io/s/react-codesandboxer-example-forked-frgdg2?file=/example.js

Thank you

react-select: 5.3.2
react: 16.14

Originally posted by @MikeYermolayev in https://github.com/JedWatson/react-select/discussions/5232

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 with the linked CodeSandbox reproduction and search the react-select source for the indexOf(selectValue[0]) focus calculations described in the issue. Confirm the behavior when options are recreated on render, then verify that focus still identifies the selected option by value rather than object reference.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.