Last active
January 11, 2024 09:54
-
-
Save victorbarretodev/782c5553b3bac62e79b1795ca3a6d134 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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