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?

More than 1 year has passed since last update.

【Next.js】Not Foundページを表示する方法

Posted at

ページを作成する

page.tsxを以下のように作成します。

page.tsx
async function getData() {
  const res = await fetch('https://api.example.com/...')

  return res.json()
}
 
export default async function Page() {
  const data = await getData()
  if (!data) {
    notFound()
  }
 
  return <main></main>
}

Not Found画面を作成する

pages.tsxと同じ階層にnot-found.tsxを作成します。

not-found.tsx
import { useEffect } from 'react'
 
export default function NotFound() {
  return (
    <div>
      <h2>ページが見つかりませんでした。</h2>
    </div>
  )
}

上記のように記述するとpage.tsxnotFound()が実行されるとnot-found.tsxが表示されるようになります。

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?