Created
September 18, 2020 08:52
-
-
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
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
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