Skip to content

Instantly share code, notes, and snippets.

@andersonFaro9
Created February 5, 2023 12:41
Show Gist options
  • Save andersonFaro9/b313f0862c33343531833d94e6a21c9c to your computer and use it in GitHub Desktop.
Save andersonFaro9/b313f0862c33343531833d94e6a21c9c to your computer and use it in GitHub Desktop.
import Head from 'next/head'
import Image from 'next/image'
import { useEffect, useState } from 'react';
import {api} from './api/api'
interface IBooks {
title: string;
}
export default function Home() {
const [books, setBook] = useState<IBooks>();
const getBooks = async () => {
try {
const myBooks = await api.get("/books").then((response) => setBook(response.data))
console.log(myBooks)
}catch(err) {
console.error(err)
}
}
useEffect( () => {
getBooks()
}, [])
return (
<>
<ul>
{books?.title}
{/* {books.map((book) =>{<li key = {book.id}>{book.title}</li>})} */}
</ul>
</>
)
}
// O axios
import axios from 'axios';
export const api = axios.create({
baseURL: 'http://localhost:3000',
})
export default api;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment