I'm using the Material UI Autocomplete component to make an API call in a method that returns a JSON. The idea is that when the user types in the autocomplete component it makes a call to find the results matching the string.
The problem I have with the code I put is that I don't know how to make the API call and return the results in the autocomplete component
const [itemsAutocomplete, setItemsAutocomplete] = useState([])
<Autocomplete
disablePortal
id="autocomplete-search"
onChange={handleItemsOptions}
getOptionLabel={option => option.name}
sx={{ width: 300 }}
renderInput={params => (
<TextField {...params} label="Search an item..." />
)}
/>
const handleItemsOptions = event => {
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name : search_name,
}),
}
fetch(
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/product/items/search/`,
requestOptions,
)
.then(response => response.json())
.then(json => setItemsAutocomplete(json))
}