0
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?

Next.jsの_app.tsxでdefault exportを使用しなくてはいけない理由

Last updated at Posted at 2024-08-08

アジェンダ

Next.jsでは、_app.tsxファイルが全てのページのエントリーポイントとして機能します。
このファイルでdefault exportを使用しなければならない理由を簡単にまとめます。

_app.tsxの役割

_app.tsxは、アプリ全体に共通するレイアウトや状態管理、CSSスタイルを設定するために使用されます。すべてのページコンポーネントは、このファイルを経由してレンダリングされます。

import type { AppProps } from 'next/app';

function MyApp({ Component, pageProps }: AppProps) {
  return <Component {...pageProps} />;
}

export default MyApp;

default exportが必要な理由

必要な理由は、一つだけなのですが、Next.jsは、_app.tsxからdefault exportされたコンポーネントを探し、エントリーポイントとして使用します。default exportがないと、Next.jsは正しく動作しません。
named exportだと以下のエラーを吐きます。

Error: The default export is not a React Component in page: "/_app"

必要な理由はこれだけになります。

0
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
0
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?