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.

Debouncing support

See original GitHub issue

Hi there, I was wondering there is a way to “debounce” async option loading with react-select. Currently, I’m seeing queries like this:

image

I would like react-select to only query after a 250ms typing pause. What do I need to do to make react-select do this? I have tried wrapping my function in a setTimeout, but that does not appear to do anything:

  taxons (input, callback) {
    setTimeout(function () {
      $.ajax({
        url: '/api/v2/taxons/search',
        dataType: 'json',
        cache: false,
        data: { name: input },
        success: function (response) {
          var options = response.data.map(function(taxon, index) {
            return (
              { value: taxon.id, label: taxon.attributes.tree_name }
            );
          });

          callback(null, {
            options: options
          })
        }
      })
    }, 250)
  }

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:14
  • Comments:44 (5 by maintainers)

github_iconTop GitHub Comments

184reactions
paulirwincommented, Feb 22, 2018

A built-in debounce prop on the Async component would be a very welcome change.

79reactions
irundaiacommented, Sep 1, 2016

@grandmore I’ve got debouncing working. The key is to make sure that lodash’s debounce method is evaluated exactly once.

Suppose you have a function fetch that retrieves the options for you and returns a Promise. You can then create a debounced version of this function like:

const debouncedFetch = debounce((searchTerm, callback) => {
  fetch(searchTerm)
    .then((result) => callback(null, {options: result})
    .catch((error) => callback(error, null));
}, 500);

This debouncedFetch function can then be passed to the Select.Async component:

<Select.Async
  ...
  loadOptions={debouncedFetch}
  ...
/>

Note that you cannot inline the debouncedFetch function in the render function of your component. If you do this, lodash’s debounce would be called whenever the component is rerendered. This breaks the debounce function.

Hope this helps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Contact - DeBounce
To open a support ticket, please send an email to support@debounce.io and our staff will respond to you shortly. Typically, messages will be...
Read more >
What is debouncing? – TechTarget Definition
Debouncing is removing unwanted input noise from buttons, switches or other user input. Debouncing prevents extra activations or slow functions from ...
Read more >
Debouncing via Software - Digilent Reference
Software debouncing is accomplished by taking multiple samples of the input signal and determining whether to assert an output signal (the debounced version...
Read more >
Definition of Debounce - Analog Devices
A debouncing circuit removes the resulting ripple signal, and provides a clean transition at its output. Synonyms. Debounced. Find a term alphabetically: Select ......
Read more >
A Guide to Debouncing - Oregon State University
Change the two hex constants to accommodate different bounce times and timer rates. // Service routine called by a timer interrupt bool_t DebounceSwitch2()....
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