Created
August 7, 2022 00:53
-
-
Save szaranger/e2a4a167a86d63f849e3d845c7a443bb 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