Last active
October 22, 2020 06:44
-
-
Save dabit3/3639c45cf5b47d361333b2c35b4f0a02 to your computer and use it in GitHub Desktop.
Next.js + Amplify - pages/posts/[id].js part 1
This file contains 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
// pages/posts/[id].js | |
import { API } from 'aws-amplify'; | |
import { getPost, listPosts } from '../../src/graphql/queries' | |
import '../../configureAmplify'; | |
import { useRouter } from 'next/router'; | |
export default function Post({ post }) { | |
const router = useRouter(); | |
if (router.isFallback) return <div>Loading...</div> | |
return ( | |
<div> | |
<h2>{post.name}</h2> | |
<p>{post.content}</p> | |
<span>By: {post.username}</span> | |
</div> | |
) | |
} | |
export async function getStaticPaths() { | |
const postData = await API.graphql({ query: listPosts }); | |
const postIds = postData.data.listPosts.items.map(post => ({ params: { id: post.id } })); | |
return { | |
paths: postIds, fallback: true | |
}; | |
} | |
export async function getStaticProps(context) { | |
const id = context.params.id; | |
const post = await API.graphql({ query: getPost, variables: { id } }); | |
return { | |
props: { | |
post: post.data.getPost | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment