Skip to content

Commit

Permalink
fix: fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
N0N4T0 committed May 7, 2024
1 parent 175f148 commit fa8de66
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Acesso from '@/components/acesso';
import ServerFetch from '@/components/server-fetch';

export default function HomePage() {
return (
<main>
<h1>Home</h1>
<Acesso />
<ServerFetch />
</main>
);
}
5 changes: 2 additions & 3 deletions src/app/sobre/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import dynamic from 'next/dynamic';
const Width = dynamic(() => import('@/components/width'), { ssr: false });
import ClientFetch from '@/components/client-fetch';

export default function SobrePage() {
return (
<main>
<h1>Sobre</h1>
<Width />
<ClientFetch />
</main>
);
}
29 changes: 29 additions & 0 deletions src/components/client-fetch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use client';

import React from 'react';

type Produto = {
id: number;
nome: string;
};

export default function ClientFetch() {
const [data, setData] = React.useState<Produto[]>([]);

React.useEffect(() => {
async function fetchData() {
const response = await fetch('https://api.origamid.online/produtos');
const json = (await response.json()) as Produto[];
setData(json);
}
fetchData();
}, []);

return (
<ul>
{data.map((produto) => (
<li key={produto.id}>{produto.nome}</li>
))}
</ul>
);
}
3 changes: 0 additions & 3 deletions src/components/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ export default function Menu() {
<li>
<Link href="/sobre">Sobre</Link>
</li>
<li>
<Link href="/imc">IMC</Link>
</li>
</ul>
);
}
17 changes: 17 additions & 0 deletions src/components/server-fetch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type Produto = {
id: number;
nome: string;
};

export default async function ServerFetch() {
const response = await fetch('https://api.origamid.online/produtos');
const data = (await response.json()) as Produto[];

return (
<ul>
{data.map((produto) => (
<li key={produto.id}>{produto.nome}</li>
))}
</ul>
);
}

0 comments on commit fa8de66

Please sign in to comment.