Error: You are calling ReactDOMClient.createRoot() on a container that has already been passed to createRoot() before の原因と解決法
Error: You are calling ReactDOMClient.createRoot() on a container that has already been passed to createRoot() before の原因と具体的な解決手順をまとめました。
エラーメッセージ(全文)
Error: You are calling ReactDOMClient.createRoot() on a container that has already been passed to createRoot() before
このエラーとは
Error: You are calling ReactDOMClient.createRoot() on a container that has already been passed to createRoot() before は React 開発でよく遭遇するエラーです。
想定環境: Windows / macOS / Linux
よく出る状況: React / Next.js / フロントエンド演習
よくある原因
- Hooks ルール違反
- SSR/CSR 不一致
- Router 設定
- Context 未設定
解決方法
方法1: 開発モードで確認
npm run dev で非 minify エラーを読む.
それでも直らないとき
バージョン情報(React のバージョン、OS、実行コマンド)を添えて、エラーメッセージ全文と直前に変更した点を確認してください。ログの数行上にも原因の手がかりが残っていることが多いです。
まとめ
「Error: You are calling ReactDOMClient.createRoot() on a container that has already been passed to createRoot() before」は Hooks ルール違反 が原因のことが多いです。上記の手順で切り分けてください。
次にやること
同じ React カテゴリの関連エラーもあわせて確認してください。
よくある質問
Error: You are calling ReactDOMClient.createRoot() on a container that has al… とは何ですか?
Error: You are calling ReactDOMClient.createRoot() on a container that has already been passed to createRoot() before は React 実行時に表示されるエラーです。Hooks ルール違反
Error: You are calling ReactDOMClient.createRoot() on a container that has al… の原因は?
Hooks ルール違反。SSR/CSR 不一致。Router 設定。
Error: You are calling ReactDOMClient.createRoot() on a container that has al… の直し方は?
開発モードで確認:npm run dev で非 minify エラーを読む.
React で Error が出るのはなぜ?
React / Next.js / フロントエンド演習
Error: You are calling ReactDOMClient.createRoot() on a container that has al… を防ぐには?
。
次に読むべき記事
- Warning: Legacy context API has been detected within a strict-mode tree の原因と解決法
- Error: Failed prop type: Invalid prop `size` of type `number` supplied to `Button`, expected `string` の原因と解決法
- Error: React.Children.only expected to receive a single React element child の原因と解決法
- Error: Cannot read properties of undefined (reading 'Provider') — React Context の原因と解決法
- Error: Cannot update during an existing state transition (such as within `render`) の原因と解決法
同カテゴリ「Reactエラー」の記事
- TypeError: Cannot read properties of null (reading 'useContext') の原因と解決法
- TypeError: Cannot read properties of null (reading 'useState') の原因と解決法
- ESLint react-hooks/rules-of-hooks: React Hook "useState" is called conditionally の原因と解決法
- Error: Invalid src prop on `next/image`, hostname "example.com" is not configured under images in your `next.config.js` の原因と解決法
- React Query Error: Query data cannot be undefined for queryKey ['items', 539] の原因と解決法
- Next.js Error: Route "/api/items/537" does not export a GET handler の原因と解決法
人気記事
- TS2307: Cannot find module '@components/Widget601' or its corresponding type declarations の原因と解決法
- TS2307: Cannot find module '@components/Widget591' or its corresponding type declarations の原因と解決法
- TS2307: Cannot find module '@components/Widget581' or its corresponding type declarations の原因と解決法
- TS2307: Cannot find module '@components/Widget571' or its corresponding type declarations の原因と解決法
- TS2307: Cannot find module '@components/Widget561' or its corresponding type declarations の原因と解決法
- TS2307: Cannot find module '@components/Widget551' or its corresponding type declarations の原因と解決法
学習ルート
体系的に学びたい方はこちらから。
エラー・トラブル集で続きを探す →あわせて読みたい
- Error: Cannot read properties of undefined (reading 'Provider') — React Context の原因と解決法同カテゴリの関連エラー
- Error: Cannot update during an existing state transition (such as within `render`) の原因と解決法同カテゴリの関連エラー
- Error: Formik: Invalid input type. Expected a string or number but got: undefined の原因と解決法同カテゴリの関連エラー
- Error: input is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML` の原因と解決法同カテゴリの関連エラー
- Error: The tag <custom-element> is unrecognized in this browser の原因と解決法同カテゴリの関連エラー