A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://nextjs.org/docs/pages/api-reference/functions/get-initial-props below:

Website Navigation


Functions: getInitialProps | Next.js

getInitialProps

Good to know: getInitialProps is a legacy API. We recommend using getStaticProps or getServerSideProps instead.

getInitialProps is an async function that can be added to the default exported React component for the page. It will run on both the server-side and again on the client-side during page transitions. The result of the function will be forwarded to the React component as props.

import { NextPageContext } from 'next'
 
Page.getInitialProps = async (ctx: NextPageContext) => {
  const res = await fetch('https://api.github.com/repos/vercel/next.js')
  const json = await res.json()
  return { stars: json.stargazers_count }
}
 
export default function Page({ stars }: { stars: number }) {
  return stars
}

Good to know:

Context Object

getInitialProps receives a single argument called context, which is an object with the following properties:

Name Description pathname Current route, the path of the page in /pages query Query string of the URL, parsed as an object asPath String of the actual path (including the query) shown in the browser req HTTP request object (server only) res HTTP response object (server only) err Error object if any error is encountered during the rendering Caveats

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4