import fetch from 'isomorphic-unfetch';
import Link from 'next/link';
import { VehiclePerson } from '../api/VehiclePerson';
interface ListProps {
ownersList?:VehiclePerson[]
}
export default function List({ownersList}:ListProps) {
return (
<div>
{
ownersList?.map((e, index) => (
<div key={index}>
<Link as={`/${e.vehicle}/${e.ownerName}`} href="/[abc]/[cde]">
<a>About</a>
</Link>
</div>
))
}
</div>
);
}
List.getInitialProps = async () => {
const response = await fetch('http://localhost:3000/vehicles');
const ownersList:ListProps[] = await response.json();
return {ownersList}
}
import fetch from 'isomorphic-unfetch';
import { NextPageContext } from 'next';
import { useEffect, useState } from 'react';
import { useRouter } from 'next/router';
export interface PersonProps {
ownersList?: VehiclePerson[];
}
export default function Person({ ownersList }: PersonProps) {
const [owners, setOwners] = useState(ownersList);
const router = useRouter();
useEffect(() => {
async function loadData() {
const response = await fetch('http://localhost:3000/vehicles?ownerName=' + router.query.person + '&vehicle=' + router.query.vehicle);
const ownersList: VehiclePerson[] | undefined = await response.json();
setOwners(ownersList);
}
if (ownersList?.length == 0) {
loadData();
}
}, [])
return <pre>{owners[0]?.details}</pre>;
}
Person.getInitialProps = async ({query, req}: NextPageContext) => {
console.log(query);
if (!req) {
return { ownersList: [] };
}
const response = await fetch('http://localhost:3000/vehicles?ownerName=' + query.person + '&vehicle=' + query.vehicle);
const ownersList: VehiclePerson[] | undefined = await response.json();
return { ownersList };
}