Skip to content

Commit

Permalink
feat!: add new fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
popovevgen committed Feb 6, 2024
1 parent c3de128 commit 5881df4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
8 changes: 8 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'fakestoreapi.com',
},
],
},
}

module.exports = nextConfig
20 changes: 19 additions & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Link from "next/link";

const inter = Inter({ subsets: ['latin'] })

export default function Home() {
export default function Home({product}: any) {
return (
<>
<Head>
Expand All @@ -18,6 +18,15 @@ export default function Home() {
<main className={`${styles.main} ${inter.className}`}>
Home page
<Link href="/other">Other</Link>
<div>
<h1> Welcome to My blog gallery ssg</h1>
{product.map((item: any) => (
<div key={item.id}>
<a> <h3>{item.title}</h3> </a>
<img src={item.url} />
</div>
))}
</div>
<div className={styles.description}>
<p>
Get started by editing&nbsp;
Expand Down Expand Up @@ -115,3 +124,12 @@ export default function Home() {
</>
)
}

export const getServerSideProps = async () => {
const res = await fetch('https://fakestoreapi.com/products?limit=20');
const data = await res.json();

return{
props: {product: data}
}
}
23 changes: 23 additions & 0 deletions src/pages/other.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ import Image from 'next/image'
import { Inter } from 'next/font/google'
import styles from '@/styles/Home.module.css'
import Link from "next/link";
import {useEffect, useState} from "react";

const inter = Inter({ subsets: ['latin'] })

export default function Home() {
const [state, setState] = useState([]);
async function getData() {
const res = await fetch('https://fakestoreapi.com/products?limit=8');
const data = await res.json();
setState(data);
}

useEffect(() => {
getData();
}, [])
return (
<>
<Head>
Expand All @@ -18,6 +29,18 @@ export default function Home() {
<main className={`${styles.main} ${inter.className}`}>
Other page
<Link href="/">Home</Link>
<div>
{
state.map((e: any) => (
<a key={e.id}>
<h2> {e.title} &rarr;</h2>
<img src={e.image} width={250} height={200}/>
<p>{e.description}</p>
<h3>${e.price}</h3>
</a>
))
}
</div>
<div className={styles.description}>
<p>
Get started by editing&nbsp;
Expand Down

0 comments on commit 5881df4

Please sign in to comment.