Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Docs] Migrate data-fetch example to typescript (#39852)
## Changelog - Updated deps - Migrated to Typescript - Replaced `div` with `Fragment` ## Documentation / Examples - [x] Make sure the linting passes by running `pnpm lint` - [x] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
- Loading branch information
Showing
5 changed files
with
68 additions
and
32 deletions.
There are no files selected for viewing
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
31 changes: 16 additions & 15 deletions
31
examples/data-fetch/pages/index.js → examples/data-fetch/pages/index.tsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
</> | ||
) | ||
} |
29 changes: 15 additions & 14 deletions
29
examples/data-fetch/pages/preact-stars.js → examples/data-fetch/pages/preact-stars.tsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
</> | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |