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 Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in

Last updated at Posted at 2026-01-07

はじめに

react-router-domのSwitchを記述するとエラーが発生した。

問題

import { BrowserRouter, Link, Switch, Route } from "react-router-dom";

(略)
 <Switch>
        <Route exact path="/">
          <Home />
        </Route>
 </Switch>

解決方法

react-router-domのバージョンが古い場合(ver5.3)はSwitchがあったが、最新盤ではSwitchの代わりにRoutesが使用される。
そのため、

 <Routes>
        <Route path="/" element={<Home />} />
 </Routes>

と記載する。

おわりに

どのバージョンを使用しているかを確認しよう。

参考

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?