rsnext/examples/api-routes/pages/index.tsx
Max Proske 1d8df75bfd
chore(examples): Convert api-routes example to TypeScript (#38083)
Converted API Routes example over to TypeScript to match the Contribution guidelines.

## 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)
2022-07-11 10:40:52 +00:00

20 lines
497 B
TypeScript

import useSWR from 'swr'
import PersonComponent from '../components/Person'
import { Person } from '../interfaces'
const fetcher = (url: string) => fetch(url).then((res) => res.json())
export default function Index() {
const { data, error } = useSWR('/api/people', fetcher)
if (error) return <div>Failed to load</div>
if (!data) return <div>Loading...</div>
return (
<ul>
{data.map((p: Person) => (
<PersonComponent key={p.id} person={p} />
))}
</ul>
)
}