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

【エラー対応】Uncaught Error: XXX is not a XXX component. All component children of XXX must be a XXX or XXX

Last updated at Posted at 2025-05-05

React Routerに関するエラー対応に関して記載。
スクリーンショット 2025-05-05 21.10.43.png

エラー

ReactでRouteを使用して開発を進めていたところ次のエラーが発生した。
他の人が10分でも時間消費せずに済むように解決方法を記述する。

Uncaught Error:
XXX is not a XXX component. All component children of XXX must be a XXX or XXX

XXXはそれぞれ任意の値


実際にエラーが発生したソースはこれ。

      <Routes>
        <Route path="/page1" />
          <Page1 />
      </Routes>

解決方法

エラー解消ソースはこれ。

      <Routes>
        <Route path="/page1" element={<Page1 />} />
      </Routes>

Routeのelement属性の中に書くようにした。

原因

どうやら、<Routes> の中には <Route> または <React.Fragment> しか書いてはいけないようだ。
React Router v6からこのようなルールになったそうだ。
ちなみに、elementではなくrender属性で書くのもダメ。elementのみ。

参考

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