Skip to content

Instantly share code, notes, and snippets.

@andersonFaro9
Created February 22, 2023 20:14
Show Gist options
  • Save andersonFaro9/bd67716b1ec7f9c7f81a59d1427ca4f7 to your computer and use it in GitHub Desktop.
Save andersonFaro9/bd67716b1ec7f9c7f81a59d1427ca4f7 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 {
id:string;
title:string;
author:string;
year:string;
bar_code:string;
description:string;
}
export default function Home() {
const [books, setBooks] = useState<IBooks[]>([]);
useEffect ( () =>{
getBooks()
postBooks()
}, [])
async function postBooks() {
try {
const myBooks = await api
.post('/books')
.then((response) => setBooks(response.data))
console.log(myBooks)
} catch (err) {
console.error(err)
}
}
async function getBooks () {
try {
const myBooks = await api
.get('/books')
.then((response) => setBooks(response.data))
console.log(myBooks)
} catch (err) {
console.error(err)
}
}
return (
<>
<ul>
{books.map((item) => (
<li className=' flex flex-col leading-10' key={item.id}>
<span className=' text-2xl'>
Titulo: {item.title} <br />
</span>
Autor:
{item.author}
<br />
Bar code:
{item.bar_code}
<br />
Ano:
{item.year}
<br />
Descrição:
{item.description}
</li>
))}
</ul>
</>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment