Skip to content

Instantly share code, notes, and snippets.

@Dev4ster
Last active January 11, 2024 09:54
Show Gist options
  • Save Dev4ster/782c5553b3bac62e79b1795ca3a6d134 to your computer and use it in GitHub Desktop.
Save Dev4ster/782c5553b3bac62e79b1795ca3a6d134 to your computer and use it in GitHub Desktop.
import React from 'react';
import AsyncSelect from 'react-select/async';
export default function PageComponent() {
const mapResponseToValuesAndLabels = (data) => ({
value: data.id,
label: data.name,
});
async function callApi(value) {
const data = await fetch(`https://jsonplaceholder.typicode.com/users`)
.then((response) => response.json())
.then((response) => response.map(mapResponseToValuesAndLabels))
.then((final) =>
final.filter((i) => i.label.toLowerCase().includes(value.toLowerCase()))
);
return data;
}
return (
<div>
<p>Exemplo de Async Select com api</p>
<AsyncSelect
cacheOptions
loadOptions={callApi}
onInputChange={(data) => {
console.log(data);
}}
onChange={(data) => {
console.log(data);
}}
defaultOptions
/>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment