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 react-select fetching on every keystroke

See original GitHub issue

I love the ease of styling and functionality of react select. Props to you guys for doing an excellent job.

Recently I was requested to add an async option where I would fetch options if the option list amount was greater than 20 (all handled in the backend). I found it works great! However, the axios call is firing on every keystroke.

I’ve tried long and dead hours to figure this out. I’m almost there, but I’m stuck on this.

oncall is defined before the definition of the function component as oncall = false.

const promiseOptions = input =>
        new Promise( resolve => {
            setTimeout( () => {
                if (!oncall) {
                    oncall = true;
                    resolve(
                        axios.get(optionsUrl)
                            .then((response) => {
                                console.log(response)
                                oncall = false;
                                return response.data.data.filter(i => i.label.toLowerCase().includes(input));
                            })
                            .catch((error) => {
                                console.log(error);
                            })
                    );
                }
            }, 1000);
        });

This code works sometimes. It would be great if I can start a call after entering 3 letters, set a timer to call again only after the user has stopped typing for 1 second. I’ve managed to do this with ‘onkeyup’ on another search input in my app that doesn’t use react-select. This plugin lacks the ‘onkeyup’ prop. I’ve also tried with settimeout without avail.

I appreciate any help.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
jisbruzzicommented, Feb 4, 2021

I expected react-select async to have some sort of “cooldown” parameter to prevent browser from firing too many xhr requests. For better workarounds check #614 .

The best solution I found was @ebonow 's codesandbox, linked in #614 : https://codesandbox.io/s/react-select-async-debounce-usecallback-oeixm

The devs should include a note in the docs addressing this issue, as including this feature in the library might be quite complex.

0reactions
ebonowcommented, Feb 4, 2021

Agreed. I have a point in the open docs improvement thread for this among a few other things that could use more visibility… https://github.com/JedWatson/react-select/issues/3006#issuecomment-761839076

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async
A flexible and beautiful Select Input control for ReactJS with multiselect, autocomplete and ajax support.
Read more >
Async Data Fetching with React-Select
In this tutorial, we will build a customized input component in a project using React-Select. With it, we will demonstrate different select ...
Read more >
React-Select Async loadOptions is not loading options ...
i am using debounce to limit the backend call, otherwise there may a backend call for every key-stroke that surely i don't want....
Read more >
How to use the react-select.Async function in ...
To help you get started, we've selected a few react-select.Async examples, based on popular ways it is used in public projects.
Read more >
React Autocomplete component - Material UI
It's meant to be an improved version of the "react-select" and "downshift" ... If your logic is fetching new options on each keystroke...
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