What does NextPage in Next.js do?
Asked Answered
I

2

11

I just started using TypeScript with Next. What does this do? import { NextPage } from 'next'; export const Page: NextPage = () => {} I looked at the docs but it does really give a good explanation in what it does.

I am guessing by the name that it routes pages. Can someone please explain what this does?

Thank you.

Ineducable answered 9/12, 2021 at 8:43 Comment(2)
Does this help answer your question: Usage of NextPage type in Next.js?Tramline
Its not next page, but Next™ PageBritain
A
10

My advice is to start here, but in brief:

import { NextPage } from 'next';

export const Page: NextPage = () => {}

NextPage is a type exported by NextJS. When we write Page: NextPage we're saying that our Page component is of type NextPage.

Answer answered 9/12, 2021 at 8:48 Comment(2)
oh i see... we are assigning our page with types.... so everything that will be a Page(in JS) will be a NextPage in TS...Ineducable
I've been learning React and NextJs and I see most people don't use this format. Just create export default functions. I only seen this in the NextJs MaterialUI example I'm working with now. Can you tell me if there's any benefit of using the above? ThanksCollector
F
2

import { NextPage } from 'next'; : This line imports the NextPage type from the next package. The NextPage type is a generic type provided by Next.js for defining the type of a Next.js page component.

When you create a page in Next.js, you typically define it as a React component. The NextPage type is a type alias for a React component that is a Next.js page. It includes properties specific to Next.js pages, such as getStaticProps, getServerSideProps, and others used for data fetching during page rendering.

So, in summary, the code you provided is creating a Next.js page component using TypeScript. It is defining a constant named Page with the type NextPage and providing the logic for your page within the arrow function. This is a common pattern when working with Next.js and TypeScript to ensure type safety and provide better development tooling support.

Fork answered 31/1 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.