b8efd80c16
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>
24 lines
586 B
TypeScript
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>
|
|
)
|
|
}
|