Life is Really Short, Have Your Life!!

ござ先輩の主に技術的なメモ

Next.js のgetStaticPropsのチュートリアルをTypeScriptで

nextjs.org

以下の所をTypeScriptで書くとこうなったので、共有。

function Blog({ posts }) {
  return (
    <ul>
      {posts.map((post) => (
        <li>{post.title}</li>
      ))}
    </ul>
  )
}

// This function gets called at build time
export async function getStaticProps() {
  // Call an external API endpoint to get posts
  const res = await fetch('https://.../posts')
  const posts = await res.json()

  // By returning { props: { posts } }, the Blog component
  // will receive `posts` as a prop at build time
  return {
    props: {
      posts,
    },
  }
}

export default Blog

gist.github.com