-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Async Debounce for Select 2.0 not behaving as expected #3075
Comments
Great reproduction! It doesn't appear to me that this issue has to do with the cache. The issueIt seems to me the issue is with an inaccurate expectation of how Lodash's debounce works. Lodash specifies that
Not that:
This means each call which is within the wait period to our debounced An exampleHere is a forked example making it less react-select specific (removes the cacheOptions and defaultOptions props, passes the option {leading: true} to the debounce function, and makes the wait period longer): https://codesandbox.io/s/olmjz7mn9z User types
User then types
A solutionUsing a promise-returning debounce method where subsequent calls return promises which will resolve to the result of the next func invocation. Updated example using |
@craigmichaelmartin Thanks for the clear explanation. Using |
@craigmichaelmartin |
Another approach, that does seem to work with lodash's debounce, is to invoke the constructor(props) {
super(props);
const wait = 1000; // milliseconds
const loadOptions = (inputValue, callback) => {
this.getAsyncOptions(inputValue)
.then(results => callback(results))
// Explicitly not returning a Promise.
return;
}
this.debouncedLoadOptions = _.debounce(loadOptions, wait);
} The tricky part is that it's easy to accidentally return a promise when you're calling an async search function, if you use an arrow function or an async/await function. |
@craigmichaelmartin Worked perfectly for my use-case. Thanks for the detailed post and sandbox! |
Using this solution, if you load options and change your input without selecting anything, it will make the second call but the subsequent results will not be handled or show up on the options list. |
Huh, I'm not experiencing that problem in my project. Maybe there's some subtlety in my particular use-case that makes it work. Well, if the technique I described doesn't work for you, then I suggest going with the approach craigmichaelmartin described, using |
We decided to use a combination of lodash/debounce and Creatable instead of AsyncCreatableSelect. This allows us to handle all the async stuff on my own component state instead of letting the Select handle it internally with all its quirks. |
Hello guys, I'm using react hooks and I'm using an async redux action to get the data when the user starts searching. It works fine and all, but the debouncing doesn't work. I'm guessing because I'm doing an async action that returns a promise. |
Try debounce-promise NPM package. |
Hello - In an effort to sustain the We understand this might be inconvenient but in the best interest of supporting the broader community we have to direct our efforts towards the current major version. If you aren't using the latest version of 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! |
Added debounce promise ass sugested by react-select community in JedWatson/react-select#3075 (comment)
As the title says, lodash (_.debounce) implementation is not working for Select 2.0 async component.
It looks like the caching mechanism is caching the previous input and displaying that in the results.
Here is my forked code example:

Does anyone have a working debounce example that works for the 2.0 implementation of React Select Async?
The text was updated successfully, but these errors were encountered: