I'm using the Google books API to fetch data. When I click the button to fetch the data the response is an empty array. I don't know what I'm doing wrong with the async function.
export default function SearchInput() {
const [inputSearch, setInput] = useState("")
const [bookSearch, setBookSearch] = useState([])
//async function to retrieve data from an API
const fetchBooks = async() => {
const res = await fetch(
`https://www.googleapis.com/books/v1/volumes?q=flowers+inauthor:keyes&key=${apikey}`
);
const data = await res.json();
setBookSearch(bookSearch);
console.log(bookSearch); //shows an empty array
}
//search function
const handleSearch = (e) => {
e.preventDefault();
fetchBooks();
}
return (
<div>
<h2>search book</h2>
<p>libro: {inputSearch}</p>
<form onSubmit={handleSearch}> // this trigger the search function
<input placeholder="Title, Author"
type="text"
value={inputSearch}
onChange={e=>{setInput(e.target.value)}}/>
<button type="submit">Search</button>
</form>
</div>
);
}