nextjs getStaticProps

nextjs

getStaticProps is a function in a Next.js page component that allows you to generate static page content at build time. It is used to fetch data that is required for a page and make that data available as a prop for the page component.

Here is an example of getStaticProps being used in a Next.js page component:

import { getStaticProps } from 'next';

function Page({ data }) {
  return <div>{data}</div>;
}

export async function getStaticProps() {
  const data = await fetchData();
  return { props: { data } };
}

export default Page;

In this example, the getStaticProps function fetches some data using the fetchData function and returns it as a prop for the Page component. The Page component then uses this data to render its content.

Note that getStaticProps can only be used in a page component and not in the main _app.js component or in custom server.js files.