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?

Error [DefaultLayout] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>

Last updated at Posted at 2026-01-08

はじめに

Routeをほかのコンポーネントで挟むとエラーになった。

問題

      <Routes>
        <DefaultLayout>
          <Route path="/" element={<Top />} />
        </DefaultLayout>
      </Routes>  

RouteはRoutes以外で挟むとエラーが発生する。

解決方法

古いReactバージョン(Ver5)では、Switchのコードで解決した

    <Switch>
   <DefaultLayout>
        <Route exact path="/">
          <Top />
        </Route>
   </DefaultLayout>
    </Switch>

今回はバージョン(Ver6)が違うため、

        <Route path="/" element={<DefaultLayout />} />

とした。
DefaultLayoutのコンポーネント内でTopコンポーネントを呼び出す方法にした。
結果、想定通りの挙動となった。

おわりに

どのバージョンを使用しているかを確認しよう。
コンポーネントの関係も把握する。

参考

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?