rsnext/examples/api-routes-rest/pages/index.tsx
Max Proske b8efd80c16
Convert api-routes-rest example to TypeScript (#38394)
Converted `api-routes-rest` example to TypeScript to match Contribution docs.

## 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)


Co-authored-by: Balázs Orbán <18369201+balazsorban44@users.noreply.github.com>
2022-07-08 11:06:40 +00:00

24 lines
586 B
TypeScript

import type { User } from '../interfaces'
import useSwr from 'swr'
import Link from 'next/link'
const fetcher = (url: string) => fetch(url).then((res) => res.json())
export default function Index() {
const { data, error } = useSwr<User[]>('/api/users', fetcher)
if (error) return <div>Failed to load users</div>
if (!data) return <div>Loading...</div>
return (
<ul>
{data.map((user) => (
<li key={user.id}>
<Link href="/user/[id]" as={`/user/${user.id}`}>
{`User ${user.id}`}
</Link>
</li>
))}
</ul>
)
}