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.

Async resets the available options after selecting and loses the selected value

See original GitHub issue

If I use the Async component to load a large filterable data source that retuns a limited amount of items, the component may lose track of the selected option (and available filters) when the user selects one.

The following CodeSandbox shows the issue:

https://lxlxj5w87q.codesandbox.io/

https://codesandbox.io/s/lxlxj5w87q

In this CodeSandbox, I created a dummy datasource that generates strings “Item 0” through “Item 9999”, filters such strings using the input provided by the loadOptions callback, and finally, limits the results to 10 items. This data source tries to mimic an API that searches a database.

I have used this data source in two scenarios:

  • A “simple” scenario: A controlled Async select;
  • Integrated with tcomb-form, which was my original use case for this component.

Both scenarios have the Async component (either directly or via tcomb-form) and a label showing the current value.

Use cases:

  • If the user selects any option that can be initially loaded, the component works as expected, calling the onChange callback with the provided option and showing the selected value.
  • However, if the user searches for 50, and selects any option, while the onChange callback is correct, the component “loses” the selected option and reloads the initial list. This seems to apply for any selected option that is not between “Item 0” and “Item 9”.

I also noticed that, when an option is selected, the loadOptions callback is called with an empty string as the term. This causes the data source to return the original list (which may not contain the selected option). If this is intended behaviour, is there a way to disable it?

After debugging for a bit it seems that loadOptions is called when the option is selected because the filter text changes.

What I did to work around this (with tcomb-form) is have my data fetching function also receive the current value (as well as the term) and have it use the value if the term is not provided. This, however, feels like a hack.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

1reaction
yozi-developercommented, Dec 19, 2017

@RedRoserade , I’ve run into this issue too and have found out this thread: https://github.com/JedWatson/react-select/issues/2004

0reactions
dhffdhcommented, Jun 26, 2019

add

onFocus={this.handleFocus}

 handleFocus = () => {
    const { selectedOption } = this.state;
    if (selectedOption && selectedOption.label) {
      this.setState({ inputValue: selectedOption.label });
    }
  };
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there any way to prevent react-select (Select.Async) from ...
I found it out: the property value of Select.Async is not the actual value of the input field. If you want to set...
Read more >
How To Handle Async Data Loading, Lazy Loading, and Code ...
This tutorial will explain how to avoid this with a special Hook called useEffect , which will run functions only when specific data...
Read more >
Advanced - React Select
A flexible and beautiful Select Input control for ReactJS with multiselect, autocomplete and ajax support.
Read more >
useForm | React Hook Form - Simple React forms validation
This option allows you to configure validation strategy when inputs with errors get ... It supports both sync and async set form default...
Read more >
JavaScript Data Grid: Set Filter - Filter List - AG Grid
The following example demonstrates sorting Set Filter values using a ... If you select any of the other options, that selection will be...
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