Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update data-fetch example to SSG #11017

Merged
merged 4 commits into from Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions examples/data-fetch/pages/index.js
Expand Up @@ -2,21 +2,25 @@ import React from 'react'
import Link from 'next/link'
import fetch from 'isomorphic-unfetch'
lfades marked this conversation as resolved.
Show resolved Hide resolved

function Index(props) {
function Index({stars}) {
return (
<div>
<p>Next.js has {props.stars} ⭐️</p>
<p>Next.js has {stars} ⭐️</p>
<Link href="/preact">
<a>How about preact?</a>
</Link>
</div>
)
}

Index.getInitialProps = async () => {
export async function getStaticProps() {
lfades marked this conversation as resolved.
Show resolved Hide resolved
const res = await fetch('https://api.github.com/repos/zeit/next.js')
const json = await res.json() // better use it inside try .. catch
return { stars: json.stargazers_count }
return {
props: {
stars: json.stargazers_count
}
}
}

export default Index
12 changes: 8 additions & 4 deletions examples/data-fetch/pages/preact.js
Expand Up @@ -2,21 +2,25 @@ import React from 'react'
import Link from 'next/link'
import fetch from 'isomorphic-unfetch'

function Preact(props) {
function Preact({stars}) {
return (
<div>
<p>Preact has {props.stars} ⭐</p>
<p>Preact has {stars} ⭐</p>
<Link href="/">
<a>I bet Next.js has more stars (?)</a>
</Link>
</div>
)
}

Preact.getInitialProps = async () => {
export async function getStaticProps() {
const res = await fetch('https://api.github.com/repos/developit/preact')
const json = await res.json() // better use it inside try .. catch
return { stars: json.stargazers_count }
return {
props: {
stars: json.stargazers_count
}
}
}

export default Preact