forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
[id].tsx
57 lines (49 loc) · 1.2 KB
/
[id].tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import Link from 'next/link'
import Head from 'next/head'
import React from 'react'
import { GetStaticProps, GetStaticPaths, NextPage } from 'next'
import { ParsedUrlQuery } from 'querystring'
import { PostData, PostDataProps } from '../../types/postdata'
import { GetPosts, GetPost } from '../../lib/postdata_api'
interface Params extends ParsedUrlQuery {
id: string
}
export const getStaticPaths: GetStaticPaths<Params> = async () => {
const postList: PostData[] = await GetPosts()
return {
paths: postList.map((post) => {
return {
params: {
id: post.id.toString(),
},
}
}),
fallback: false,
}
}
export const getStaticProps: GetStaticProps<PostDataProps, Params> = async (
context
) => {
const { id } = context.params! as Params
const postData: PostData = await GetPost(id)
return {
props: {
postData,
},
}
}
const Post: NextPage<PostDataProps> = ({ postData }: PostDataProps) => {
return (
<main>
<Head>
<title>{postData.title}</title>
</Head>
<h1>{postData.title}</h1>
<p>{postData.body}</p>
<Link href="/">
<a>Go back to home</a>
</Link>
</main>
)
}
export default Post