I am doing a personal project on Next.js with TypeScript. I am trying to fetch an api and then to do a map of the items, but I cannot make it work. I did a console.log and it gives undefined. I did put the file on the pages folder. This is the code:
import { InferGetStaticPropsType } from 'next'
type Data = {
id: string
films: string
people: string
planets: string
species: string
vehicles: string
}
export const getStaticProps = async () => {
const res = await fetch('https://www.swapi.tech/api/')
const swapis: Data[] = await res.json()
return {
props: {
swapis,
},
}
}
const FilmList = ({ swapis }: InferGetStaticPropsType<typeof getStaticProps>) => {
console.log('swapis', swapis)
return (
<>
<h2>List of Films</h2>
{swapis.map((swapi) => (
<ul key={swapi.id}>
<li>{swapi.films}</li>
</ul>
))}
</>
);
}
export default FilmList;