Async results not selectable
See original GitHub issueHello,
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:
- Created 3 years ago
- Comments:5
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
the drink is not selecting, @tbleckert this bug is not fixed
https://user-images.githubusercontent.com/8565993/104350590-53ed9a80-552a-11eb-85c6-a1d0abb5dc1f.mov