できない.dev

Next.js App Router で useState などのフックが使えない

useState などのフックが使えないときは、ファイル先頭(import より上)に use client を書けば直る。
App Router は既定でサーバーコンポーネントのため、書き忘れや位置ミスが主な原因だ。

公開: 更新:

症状できない · exit 1
npx next build
…
Failed to compile.
./app/Counter.js
Error:   x You're importing a component that needs `useState`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
解決後できた · exit 0
npx next build
…
  ├ chunks/4bd1b696-c023c6e3521b1417.js  54.2 kB
  └ other shared chunks (total)          1.88 kB
○  (Static)  prerendered as static content

2026-11-11 に node:20(Next.js 15.5.29 / Node.js 20.20.2) で実際に打って取った出力。検証環境の詳細

要約

App Router でフックが使えないエラーが出る場合、原因はほぼ「use client ディレクティブを書いていない / 位置が誤っている」のどちらかである。
サーバーコンポーネントが既定で、フックや onClick などのイベントハンドラはクライアント側でしか動かない。

実行例

"use client" を書いていない Counter.js を page.js から読み込むと、next build は useState がクライアントコンポーネントでしか動かないというエラーで終了コード 1 になった。
ディレクティブを import の下に置いた場合も、The "use client" directive must be placed before other expressions が加わって同じく失敗した。
ファイル先頭へ移すとビルドが通り、/ は静的ページとして生成された。

$ npx next build
   ▲ Next.js 15.5.29
 
   Creating an optimized production build ...
Failed to compile.
 
./app/Counter.js
Error:   x You're importing a component that needs `useState`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
  |
  |  Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
  |
 
   ,-[/tmp/tmp.LMCtyRZaNs/app/Counter.js:1:1]
 1 | import { useState } from "react";
   :          ^^^^^^^^
 2 | 
 3 | export function Counter() {
 4 |   const [n, setN] = useState(0);
   `----
 
Import trace for requested module:
./app/Counter.js
./app/page.js
 
> Build failed because of webpack errors
$ echo $?
1
$ npx next build
   ▲ Next.js 15.5.29
 
   Creating an optimized production build ...
Failed to compile.
 
./app/Counter.js
Error:   x The "use client" directive must be placed before other expressions. Move it to the top of the file to resolve this issue.
   ,-[/tmp/tmp.LMCtyRZaNs/app/Counter.js:2:1]
 1 | import { useState } from "react";
 2 | "use client";
   : ^^^^^^^^^^^^^
 3 | 
 4 | export function Counter() {
 5 |   const [n, setN] = useState(0);
   `----
  x You're importing a component that needs `useState`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
  |
  |  Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
  |
 
   ,-[/tmp/tmp.LMCtyRZaNs/app/Counter.js:1:1]
 1 | import { useState } from "react";
   :          ^^^^^^^^
 2 | "use client";
 3 | 
 4 | export function Counter() {
   `----
 
Import trace for requested module:
./app/Counter.js
./app/page.js
 
> Build failed because of webpack errors
$ echo $?
1
$ npx next build
   ▲ Next.js 15.5.29
 
   Creating an optimized production build ...
 ✓ Compiled successfully in 2.1s
   Linting and checking validity of types ...
   Collecting page data ...
   Generating static pages (0/4) ...
   Generating static pages (1/4) 
   Generating static pages (2/4) 
   Generating static pages (3/4) 
 ✓ Generating static pages (4/4)
   Finalizing page optimization ...
   Collecting build traces ...
 
Route (app)                                 Size  First Load JS
┌ ○ /                                      271 B         103 kB
└ ○ /_not-found                            996 B         104 kB
+ First Load JS shared by all             103 kB
  ├ chunks/255-73938ce8a8eac1ad.js       46.6 kB
  ├ chunks/4bd1b696-c023c6e3521b1417.js  54.2 kB
  └ other shared chunks (total)          1.88 kB
 
○  (Static)  prerendered as static content
$ echo $?
0

— 2026-11-11 時点の出力

検証環境Node.js 20.20.2 / npm 10.8.2 / Python 3.11.2 / 2026-11-11 検証(ほか 2 件)
検証日
実行環境
node:20 Debian GNU/Linux 12 (bookworm)
バージョン
  • Node.js 20.20.2
  • npm 10.8.2
  • Python 3.11.2
  • Git 2.39.5
  • Next.js 15.5.29

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. ディレクティブを正しく書く

公式の Client Components の解説(新しいタブで開く) のとおり、ファイル先頭に書く。

"use client";
 
import { useState } from "react";
 
export function Counter() {
  const [n, setN] = useState(0);
  return <button onClick={() => setN(n + 1)}>{n}</button>;
}

2. 境界を細かく分ける

page.tsx 自体はサーバーコンポーネントのままにし、インタラクション部分のみ別ファイル(例: Counter.tsx)に切り出して "use client"; を付ける。
データ取得はサーバー・クライアントの使い分け(新しいタブで開く) に従いサーバー側で完結させると配信量も減る。

3. クライアント専用ライブラリを動的読み込み

import dynamic from "next/dynamic";
 
const Editor = dynamic(() => import("./Editor"), { ssr: false });

ブラウザ API に依存するライブラリはこれでサーバー側評価を回避できる。
use client を付けたファイルから動的 import するのが定石である。

よくある原因

  1. use client を書き忘れている。
    このときは「You're importing a component that needs useState. It only works in a Client Component.」エラーが出る。
    原因と直し方は同じだが、Next.js 15.5 では文言が次のように変わっている。
    「You're importing a component that needs `useState`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.」
  2. ディレクティブの位置が誤っている。
    import の下に書くと無効になり、ビルドエラーになる。
  3. 境界の設計を誤っている。
    親をクライアント化して、本来サーバーで実行したい子コンポーネントまで巻き込んでいる。
  4. window などブラウザ API に依存するクライアント専用の SDK を、サーバー側で評価して落ちている。