To gain a deeper understanding of the NextPage
type, let's delve into its definition within Next.js.
export type NextPage<P = {}, IP = P> = NextComponentType<NextPageContext, IP, P>
This type is built upon another type called NextComponentType
, which itself is defined as follows.
export declare type NextComponentType<C extends BaseContext = NextPageContext, IP = {}, P = {}> = ComponentType<P> & {
/**
* Used for initial page load data population. Data returned from `getInitialProps` is serialized when server rendered.
* Make sure to return plain `Object` without using `Date`, `Map`, `Set`.
* @param ctx Context of `page`
*/
getInitialProps?(context: C): IP | Promise<IP>;
};
This type directly extends React's ComponentType
and includes an optional property called getInitialProps
.
In essence, the NextPage
type allows for proper typing of page components that involve using getInitialProps
(although less common due to the introduction of getServerSideProps
).
While it is possible to utilize React's standard types for a page component that does not incorporate getInitialProps
, it is advisable to use the types provided by Next.js. This will ensure that your code remains robust and aligned with future changes in the framework.