2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Next.js】getStaticPropsとは

Posted at

getStaticPropsとは

Next.jsのgetStaticPropsは、静的生成(Static Generation)を行うページで使用される関数です。この関数は、ページのビルド時にデータを取得し、ページのHTMLを生成する際にそのデータを使用します。具体的には、サーバーサイドでのデータのフェッチや処理を行い、その結果をページコンポーネントに渡します。

特徴

getStaticProps関数は次のような特徴を持ちます:

1. 静的生成とデータのフェッチ : getStaticPropsを使用すると、ページのビルド時にデータをフェッチすることができます。このデータは、ページのHTML生成時に使用され、静的なコンテンツとして提供されます。

2. 非同期処理のサポート : getStaticPropsは非同期関数として実装されます。つまり、データの取得に時間がかかる場合や外部APIからデータを取得する場合など、非同期処理を行うことができます。

3. ページの再ビルド : データが更新されるたびに、ページを再ビルドする必要があります。これにより、常に最新のデータが提供される静的なページを維持することができます。

使用方法

getStaticProps関数は、次のように実装されます:

export async function getStaticProps(context) {
  // データの取得や処理を行う
  const data = await fetchData();

  // 取得したデータをpropsとして返す
  return {
    props: {
      data
    }
  };
}

getStaticProps関数は、ページコンポーネントの外で実装されます。これにより、ページのビルド時にデータを取得し、そのデータをページコンポーネントにpropsとして渡すことができます。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?