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

[Docs] Migrate data-fetch example to typescript #39852

Merged
merged 2 commits into from Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 8 additions & 3 deletions examples/data-fetch/package.json
Expand Up @@ -6,8 +6,13 @@
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2"
"next": "12.2.5",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "18.7.11",
"@types/react": "18.0.17",
"typescript": "4.7.4"
}
}
@@ -1,25 +1,26 @@
import Link from 'next/link'

function Index({ stars }) {
return (
<div>
<p>Next.js has {stars} ⭐️</p>
<Link href="/preact-stars">
<a>How about preact?</a>
</Link>
</div>
)
}
import type { InferGetStaticPropsType } from 'next'
import type { Repository } from '../types/github'

export async function getStaticProps() {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const json = await res.json()

const data: Repository = await res.json()
return {
props: {
stars: json.stargazers_count,
stars: data.stargazers_count,
},
}
}

export default Index
export default function IndexPage({
stars,
}: InferGetStaticPropsType<typeof getStaticProps>) {
return (
<>
<p>Next.js has {stars} ⭐️</p>
<Link href="/preact-stars">
<a>How about preact?</a>
</Link>
</>
)
}
@@ -1,25 +1,26 @@
import Link from 'next/link'

function PreactStars({ stars }) {
return (
<div>
<p>Preact has {stars} ⭐</p>
<Link href="/">
<a>I bet Next.js has more stars (?)</a>
</Link>
</div>
)
}
import type { InferGetStaticPropsType } from 'next'
import type { Repository } from '../types/github'

export async function getStaticProps() {
const res = await fetch('https://api.github.com/repos/preactjs/preact')
const json = await res.json()

const json: Repository = await res.json()
return {
props: {
stars: json.stargazers_count,
},
}
}

export default PreactStars
export default function PreactStarsPage({
stars,
}: InferGetStaticPropsType<typeof getStaticProps>) {
return (
<>
<p>Preact has {stars} ⭐</p>
<Link href="/">
<a>I bet Next.js has more stars (?)</a>
</Link>
</>
)
}
19 changes: 19 additions & 0 deletions examples/data-fetch/tsconfig.json
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
10 changes: 10 additions & 0 deletions examples/data-fetch/types/github.d.ts
@@ -0,0 +1,10 @@
// For simplicity we are creating our own types here.
// If you want the full types check out:
// https://github.com/octokit/openapi-types.ts
export type Repository = {
id: number
name: string
full_name: string
stargazers_count: number
private: boolean
} & Record<string, unknown>