Created
August 6, 2022 11:00
-
-
Save szaranger/44a9d6371f0b2a0982e203563208784e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useRouter } from 'next/router'; | |
const Post = ({ post }) => { | |
const router = useRouter(); | |
if (router.isFallback) { | |
return <div>Loading...</div>; | |
} | |
return <div>{...post}</div>; | |
}; | |
export async function getStaticPaths() { | |
return { | |
paths: [{ params: { id: '1' } }, { params: { id: '2' } }], | |
fallback: true, | |
}; | |
} | |
export async function getStaticProps({ params }) { | |
// if url was /posts/2, params.id will be 2 | |
const res = await fetch(`https://path/to/your/api/${params.id}`); | |
const post = await res.json(); | |
return { props: { post } }; | |
} | |
export default Post; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment