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.

Option Does not get rendered without typing white space

See original GitHub issue

When I try to use async menus are rendered only when I type whitespace. I can verify that it is not problem of loadOptions since I can verify that function call successfully since it is called when I did not make white space at the end of my inputbox.

getAddressOpts = debounce((address) => {
    if (!address || address.length < 3) {
        return Promise.resolve({ options: [] });
    }
    let originalUrl= 'api/get_address_info';
    originalUrl = originalUrl + "/" + address
    return fetch(originalUrl)
    .then((response) =>  response.json())
    .catch((error) =>  {options:[]})
    .then((json) => {
        return {
            options: json.map((data)=>{
            var return_data = {}
            return_data['value'] = data.address
            return_data['label'] = data.address
            return return_data
        }),
    complete: true};})
},800)

<Select.Async name="address" searchPromptText="주소나 지명을 입력해주세요" cache={false} loadingPlaceholder="" isLoading={true} onChange={this.onChange} value={this.state.value} loadOptions={this.getAddressOpts} backspaceRemoves={true} />

image

image

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
malegendercommented, Jan 14, 2020

Thanks @martin-g

Don`t return Promise in debounce.

This not working:

const loadOptions = debounce(async (name) => {
    const options = await loadOptions();
    return options
  }, 1000);

and this is not working:

const loadOptions = debounce(async (name, callback) => {
    const options = await loadOptions();
    callback(options);
  }, 1000);

Working for me:

const loadOptions = debounce((name, callback) => {
    loadOptions().then(options => {
      callback(options);
    });
  }, 1000);
1reaction
reichert621commented, Jun 21, 2018

My guess is that the loadOptions function is expected to either return a promise or execute a callback, but your debounced function does neither.

It seems that in order to use _.debounce, you’ll need to use a callback. Maybe something like this:

const getAddressOptions = (address) => {
  if (!address || address.length < 3) {
    return Promise.resolve({ options: [] });
  }

  const endpoint = `api/get_address_info/${address}`;

  return fetch(endpoint)
    .then(response => response.json())
    .then(json => {
      // etc.
    });
};

const getAddressOptionsDebounced = _.debounce((input, callback) => {
  getAddressOptions(input)
    .then(result => callback(null, result))
    .catch(err => callback(err));
}, 800);

// ...

<Select.Async loadOptions={getAddressOptionsDebounced} />

Also check out https://github.com/JedWatson/react-select/issues/2476 and https://github.com/JedWatson/react-select/issues/614

Read more comments on GitHub >

github_iconTop Results From Across the Web

white-space - CSS: Cascading Style Sheets - MDN Web Docs
The white-space CSS property sets how white space inside an element is handled.
Read more >
Preserving white space in an HTML select option
When creating the select option with javascript, to preserve white-space, use "\xa0" - it is a NO-BREAK SPACE char.
Read more >
white-space - CSS-Tricks
The CSS white-space property controls how text is handled on the element it is applied to. Let's say you have HTML exactly like...
Read more >
Dealing with significant white space in HTML
CSS: whitespace: no-wrap. The next white space variation is no-wrap . This option will remove all white spaces in the text and newlines....
Read more >
Description of the "White space between pages" option in Word
In Microsoft Word, you can hide the white space at the top and bottom of each page and reduce the amount of gray...
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