Page content:
import { useRouter } from "next/router";
export default function Gunluk({ data }) {
let router = useRouter()
const { slug } = router.query;
return slug;
}
And show on the page 60d6180ea9284106a4fd8441 (https://.../gunluk/60d6180ea9284106a4fd8441 id in URL)
So I can get the ID but I don't know how to pass it to API.
export async function getStaticProps(context) {
const res = await fetch(`http://localhost:3000/api/books-i-have`)
const data = await res.json()
if (!data) {
return {
notFound: true,
}
}
return {
props: { data }, // will be passed to the page component as props
}
}
Normally I use this but it didn't work in slug. I tried both other methods but failed (https://nextjs.org/docs/basic-features/data-fetching).
In short, how do you connect to the API from the Slug page?
File directory:
pages/
gunluk/
[...slug].js
index.js