
NextJS ตัวอย่างการทำ pagination ใน getServerSideProps
มาดูตัวอย่างการทำ pagination ใน next กัน โดยจะทำใน getServerSideProps
ตัวอย่างโค้ดการทำ pagination
import { useRouter } from 'next/router'
function Page({ data }) {
// ...
}
export async function getServerSideProps(context) {
const page = context.query.page || 1
const data = await fetchData(page)
return { props: { data } }
}
จากตัวอย่างในฟังก์ชัน getServerSideProps เราใช้ context.query.page หรือ context.params เพื่อรับ dynamic router page