1d8df75bfd
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)
20 lines
497 B
TypeScript
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>
|
|
)
|
|
}
|