next-app/app/page.tsx
Alex Stan 8281d66939 Get posts from API (#2)
Co-authored-by: Alex Stan <alex.stan.2010@proton.me>
Co-committed-by: Alex Stan <alex.stan.2010@proton.me>
2024-06-01 11:08:49 +03:00

24 lines
578 B
TypeScript

import Image from "next/image";
async function getData() {
const res = await fetch('https://my-json-server.typicode.com/typicode/demo/posts');
// The return value is *not* serialized
// You can return Date, Map, Set, etc.
if (!res.ok) {
// This will activate the closest `error.js` Error Boundary
throw new Error('Failed to fetch data');
}
return res.json()
}
export default async function Home() {
const data = await getData();
return (
<main>
<p className = "text-lg font-sans hover:text-xl">{ data[ 1 ].title } </p>
</main>
);
}