0

I'm creating a book finder app using google book api. But I can get from the api only the kind value and not the title and the authors I leave here the code

    const getData = async () => {
        const response = await axios.get(url);
        console.log(response); // 2) Passo funzione utile a fetchare dei dati dall'api con axios al url
        setBooks(response.data.items);
      };
      useEffect(() => {
        // 3)Passo abbiamo settato il nuovo array dai dati presi da getData che li prendeva dall'api
        getData(); // Chiamata all'interno di una funzione in useEffect perche lo use effect non può essere asyncrono
      }, []); // Clean up cosi gli diciamo che vogliamo che venga chiamato solo al primo render se no va in loop senza
      return (
        <>
        <ul>
          {
            books.map( el => {
              const {kind, id, title} = el
              return <li key={id} className="shadow">
                <h5>{title}</h5>
              </li>
            })
          }
        </ul>
      </>
    );
    };

here the api

  "kind": "books#volumes",
  "totalItems": 2,
  "items": [
    {
      "kind": "books#volume",
      "id": "5svUwAEACAAJ",
      "etag": "ag5tZElp08M",
      "selfLink": "https://www.googleapis.com/books/v1/volumes/5svUwAEACAAJ",
      "volumeInfo": {
        "title": "Year Book",
        "subtitle": "The Annual Supplement to the World Book Encyclopedia : the 1989 World Book : a Review of the Events of 1988",
        "authors": [
          "World Book Encyclopedia"
        ],

thanks for the help

Stano
  • 1

1 Answers1

0

Just add if after useEffect, and set by default state books null

const [books, setBooks] = useState(null)
// Your code
if (!books) return <h1>Loading...</h1>
Donnle
  • 106
  • 6