Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement rolling delay before loadAsyncOptions feature #473

Closed

Conversation

oliversong
Copy link
Contributor

Addresses #459

Adds the ability to pass in a timeout prop. react-select will now wait delayAsyncMs milliseconds before calling loadAsyncOptions, and keep resetting the delay on every new input change until delayAsyncMs milliseconds passes without an input change.

Needs tests. Sorry, didn't have time to figure that out yet, but hopefully this will be useful for somebody!

}

if (this.props.asyncOptions) {
var callAsyncLoad = function() {
this.loadAsyncOptions(this._optionsFilterString, {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick note that this._optionsFilterString is preferable to event.target.value, since in the case of this function it doesn't require the argument to be redefined in its closure.

@mmahalwy
Copy link

mmahalwy commented Dec 9, 2015

any uck with this?

@tommyalvarez
Copy link

@oliversong can you fix the coverage downgrade?

@JedWatson
Copy link
Owner

Thanks for the PR @oliversong but I'd prefer this was left for users to implement in the function that's passed to loadOptions

You can do this really simply by wrapping the method in lodash's debounce method (or similar) without adding any more logic to react-select

@JedWatson JedWatson closed this Nov 27, 2017
@craigmichaelmartin
Copy link

For those doing so, be aware that Lodash's debounce function isn't suitable for this. A promise-returning debounce method where subsequent calls return promises which will resolve to the result of the next func invocation in needed. See #3075 (comment)

@ebonow
Copy link
Collaborator

ebonow commented Jan 15, 2021

For those doing so, be aware that Lodash's debounce function isn't suitable for this. A promise-returning debounce method where subsequent calls return promises which will resolve to the result of the next func invocation in needed. See #3075 (comment)

For those looking for a solution, please feel free to try the solution suggested here: #614 (comment)

const loadOptions = React.useCallback(
  debounce((inputValue, callback) => {
    getOptions(inputValue).then(options => callback(options))
  }, 500),
  []
);

return <AsyncSelect loadOptions={loadOptions} {...otherProps} />

Demo: codesandbox

@AndrejGajdos
Copy link

@ebonow take into account useCallback throws React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead. warning

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants