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】Missing Suspense boundary with useSearchParams 解消法

Posted at

解消法

useSearchParamsが使用されているコンポーネントを以下のように<Suspense>で括ることでエラーが解消します。

'use client'
 
import { useSearchParams } from 'next/navigation'
import { Suspense } from 'react'
 
function Search() {
  const searchParams = useSearchParams()
 
  return <input placeholder="Search..." />
}
 
export function Searchbar() {
  return (
    <Suspense>
      <Search />
    </Suspense>
  )
}

原因

ルートが静的にレンダリングされる場合にuseSearchParamsを呼び出すと、最も近いSuspense境界までがクライアント側でレンダリングされます。
そのため、Suspenseで括らない場合にページ全体がクライアント側でレンダリングされるようになり、JavaScriptの読み込みが完了するまで空白のページが表示されてしまう可能性があります。
これを避けるため、静的レンダリングのルート内にuseSearchParamsが使用されていてSuspenseで括られていないコンポーネントが存在する場合にタイトルのエラーが発生します。

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?