Skip to content

Instantly share code, notes, and snippets.

@sparrow
Created September 18, 2020 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sparrow/fc70b60a9e3014bc6254a60040f6a478 to your computer and use it in GitHub Desktop.
Save sparrow/fc70b60a9e3014bc6254a60040f6a478 to your computer and use it in GitHub Desktop.
This code snippet is an example of using getStaticProps to pre-render a React application page. This code snippet is from the article originally published on RubyGarage’s blog: https://rubygarage.org/blog/how-to-integrate-ssr-for-react-app
function Blog({ posts }) {
// Render posts...
}
// 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment