Không biết sử dụng tham số thứ 2 là req trong getInitialProps làm gì vì bỏ đi nó hoạt động tốt :( khi click vô link nó không bị trắng :)
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);
return <pre>{owners[0]?.details}</pre>;
}
Person.getInitialProps = async ({query, req}: NextPageContext) => {
const response = await fetch('http://localhost:3000/vehicles?ownerName=' + query.person + '&vehicle=' + query.vehicle);
const ownersList: VehiclePerson[] | undefined = await response.json();
return { ownersList };
}