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 results not selectable

See original GitHub issue

Hello,

Just wanted to mention that I had some difficulty implementing the async promise-based approach to using this library.

The issue I was having is that when a user selects an option that was populated by async API, it doesn’t appear to properly select, and doesn’t fire any events.

This behavior can be seen here: https://react-select-search.com/?path=/story/async--fetch

The way I ended up fixing this is by additionally writing the results for the API call to an object I passed to options, instead of passing an empty array as the example above models.

i.e.:




useEffect(() => {
  ApiClient.get("/search")
    .then((res) => setallStocks(formatStocks(res.data)))
    .catch(console.log);
}, []);

const matchStocks = function (searchTerm) {
    // console.log("searchTerm is ", searchTerm, typeof searchTerm);
    searchTerm = searchTerm.toUpperCase();
    var filteredArray = allStocks.filter(function (obj) {
      return obj.value.includes(searchTerm);
    });

    return filteredArray;
  };

const formatStocks = function (data) {
  console.log("unformatted data is ", data);

  let array = [];

  data.forEach(function (el) {
    array.push({
      name: el["name"],
      value: el["name"],
      exchange: el["exchange"]
    });
  });

  // console.log("allstocks is", array);
  console.log("formatted data is ", array);
  return array;
};



const handleSelect = function (selectedStock) {
  console.log("handling change", selectedStock);

  // unfortunately, search module just returns value, not obj, so we have to search again to find exchange

  var theStock = matchingStocks.filter(function (obj) {
    return obj.value == selectedStock;
  });

    console.log(exchangeCode);

    console.log(history);
    history.push({
      pathname: `/stock/${exchangeCode}:${theStock[0]["name"] + "/D"}`
    });
  }
};

const getOptions = function (query) {
  let results = matchStocks(query);
  setMatchingStocks(results);
  return results;
};


return (
  <div class="topBar">
    <SelectSearch
      options={matchingStocks}
      onChange={handleSelect}
      getOptions={(query) => {
        return new Promise((resolve, reject) => {
          resolve(getOptions(query));
        });
      }}
      search
      autoComplete="false"
      placeholder="Search..."
    />
  </div>


Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
bansalvkscommented, Jan 13, 2021

the drink is not selecting, @tbleckert this bug is not fixed

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async not displaying results - Stack Overflow
I am trying to make my first attempt with Async from react-select and I can't make Async display ...
Read more >
Do not use forEach with async-await - gists · GitHub
Array.prototype.forEach is not designed for asynchronous code. (It was not suitable for promises, and it is not suitable for async-await.).
Read more >
await - JavaScript - MDN Web Docs
The await operator is used to wait for a Promise and get its fulfillment value. It can only be used inside an async...
Read more >
Asynchronous programming: futures, async, await | Dart
This codelab teaches you how to write asynchronous code using futures and the async and await keywords. Using embedded DartPad editors, you can...
Read more >
Task asynchronous programming model - Microsoft Learn
A synchronous method returns when its work is complete (step 5), but an async method returns a task value when its work is...
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