Error: [undefined] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>
Error: [undefined] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>
我正在尝试将 react-router-dom 从 v5 升级到 v6,但我收到此错误消息。
Error: [undefined] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>
任何人都可以帮助我解决这个问题。
index.js 文件代码
import React from "react"
import ReactDOM from "react-dom"
import "./assets/main.css"
import App from "./App"
import { BrowserRouter } from "react-router-dom"
import { AuthContextProvider } from "./context/AuthContext"
ReactDOM.render(
<React.StrictMode>
<AuthContextProvider>
<BrowserRouter>
<App />
</BrowserRouter>
</AuthContextProvider>
</React.StrictMode>,
document.getElementById("root")
)
App.js 文件代码
import React, { Suspense } from "react"
import { Routes, Route } from "react-router-dom"
import "./App.css"
import { Helmet } from "react-helmet"
// custom components
import SideBar from "./components/layouts/SideBar"
import Navbar from "./components/layouts/Navbar"
import Footer from "./components/layouts/Footer"
import Feedback from "./components/feedback/Feedback"
import useGaTracker from "./hooks/useGaTracker"
import LottieAnimation from "./components/smallComponents/LottieAnimation"
// loader
import rocketLoader from "./assets/animated_illustrations/loader.json"
// lazy loading components
const Dashboard = React.lazy(() => import("./components/dashboard/Dashboard"))
const ChallengesList = React.lazy(() => import("./components/challenges/ChallengesList"))
const ChallengeDetails = React.lazy(() =>
import("./components/challenges/ChallengeDetails")
)
const Resources = React.lazy(() => import("./components/resources/Resources"))
const Roadmaps = React.lazy(() => import("./components/roadmaps/Roadmaps"))
const SolutionList = React.lazy(() => import("./components/solutions/SolutionList"))
const MySolutions = React.lazy(() => import("./components/MySolutions/MySolutions"))
const App = () => {
useGaTracker()
return (
<>
<Helmet>
<title>CODINGSPACE - Learn by Building Web and Mobile Apps</title>
</Helmet>
<div className="relative grid min-h-screen md:grid-cols-layout-tablet xl:grid-cols-layout-desktop grid-rows-layout-desktop md:gap-6">
<Navbar />
<SideBar />
<Suspense
fallback={
<div className="sm:ml-0 pr-5 py-52 row-start-2 row-end-3 col-start-1 md:col-start-2 col-end-3 place-self-center">
<LottieAnimation
animationDataFile={rocketLoader}
height={100}
width={100}
/>
</div>
}
>
<Routes>
<Route exact path="/">
<Dashboard />
</Route>
<Route path="/challenges">
<ChallengesList />
</Route>
<Route path="/challenge/:id">
<ChallengeDetails />
</Route>
<Route path="/resources">
<Resources />
</Route>
<Route path="/roadmaps">
<Roadmaps />
</Route>
<Route path="/solutions">
<SolutionList />
</Route>
<Route path="/mysolutions">
<MySolutions />
</Route>
</Routes>
</Suspense>
<Feedback />
<Footer />
</div>
</>
)
}
export default App
您应该将组件作为“元素”传递,例如:
<Route path="/" element={<Dashboard/>}>
或者对于受保护的路由:
<Route
path="home"
element={
<ProtectedRoute user={user}>
<Home />
</ProtectedRoute>
}
/>
...
</Routes>
我正在尝试将 react-router-dom 从 v5 升级到 v6,但我收到此错误消息。
Error: [undefined] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>
任何人都可以帮助我解决这个问题。
index.js 文件代码
import React from "react"
import ReactDOM from "react-dom"
import "./assets/main.css"
import App from "./App"
import { BrowserRouter } from "react-router-dom"
import { AuthContextProvider } from "./context/AuthContext"
ReactDOM.render(
<React.StrictMode>
<AuthContextProvider>
<BrowserRouter>
<App />
</BrowserRouter>
</AuthContextProvider>
</React.StrictMode>,
document.getElementById("root")
)
App.js 文件代码
import React, { Suspense } from "react"
import { Routes, Route } from "react-router-dom"
import "./App.css"
import { Helmet } from "react-helmet"
// custom components
import SideBar from "./components/layouts/SideBar"
import Navbar from "./components/layouts/Navbar"
import Footer from "./components/layouts/Footer"
import Feedback from "./components/feedback/Feedback"
import useGaTracker from "./hooks/useGaTracker"
import LottieAnimation from "./components/smallComponents/LottieAnimation"
// loader
import rocketLoader from "./assets/animated_illustrations/loader.json"
// lazy loading components
const Dashboard = React.lazy(() => import("./components/dashboard/Dashboard"))
const ChallengesList = React.lazy(() => import("./components/challenges/ChallengesList"))
const ChallengeDetails = React.lazy(() =>
import("./components/challenges/ChallengeDetails")
)
const Resources = React.lazy(() => import("./components/resources/Resources"))
const Roadmaps = React.lazy(() => import("./components/roadmaps/Roadmaps"))
const SolutionList = React.lazy(() => import("./components/solutions/SolutionList"))
const MySolutions = React.lazy(() => import("./components/MySolutions/MySolutions"))
const App = () => {
useGaTracker()
return (
<>
<Helmet>
<title>CODINGSPACE - Learn by Building Web and Mobile Apps</title>
</Helmet>
<div className="relative grid min-h-screen md:grid-cols-layout-tablet xl:grid-cols-layout-desktop grid-rows-layout-desktop md:gap-6">
<Navbar />
<SideBar />
<Suspense
fallback={
<div className="sm:ml-0 pr-5 py-52 row-start-2 row-end-3 col-start-1 md:col-start-2 col-end-3 place-self-center">
<LottieAnimation
animationDataFile={rocketLoader}
height={100}
width={100}
/>
</div>
}
>
<Routes>
<Route exact path="/">
<Dashboard />
</Route>
<Route path="/challenges">
<ChallengesList />
</Route>
<Route path="/challenge/:id">
<ChallengeDetails />
</Route>
<Route path="/resources">
<Resources />
</Route>
<Route path="/roadmaps">
<Roadmaps />
</Route>
<Route path="/solutions">
<SolutionList />
</Route>
<Route path="/mysolutions">
<MySolutions />
</Route>
</Routes>
</Suspense>
<Feedback />
<Footer />
</div>
</>
)
}
export default App
您应该将组件作为“元素”传递,例如:
<Route path="/" element={<Dashboard/>}>
或者对于受保护的路由:
<Route
path="home"
element={
<ProtectedRoute user={user}>
<Home />
</ProtectedRoute>
}
/>
...
</Routes>