I'm doing a course where version 5 of react-router-dom is used where there have been some pretty significant changes to react-router-dom.
When, launch the application, a white page appears in the browser, using devtools I get an error message: Uncaught Error: A <Route> is only ever to be used as the child of <Routes> element, never rendered directly. Please wrap your Route in a Routes.
Before I decided to write this post, I looked through various forums on how to solve this error, the two main solutions are as follows:
- changing the version of
react-router-domto an older one (I do not want to use this method, I would like to solve the problem) - as the error message suggests to wrap the
RouteinRoutes. This is what I did. But it didn't work.
App.js
import "./App.css";
import Dashboard from "./components/Dashboard";
import Header from "./components/Layout/Header";
import "bootstrap/dist/css/bootstrap.min.css";
import { BrowserRouter as Routes, Router, Route } from "react-router-dom";
import AddProject from "./components/Project/AddProject";
import { Component } from "react";
class App extends Component {
render() {
return (
<Router>
<div className="App">
<Header />
<Routes>
<Route exact path="/dashboard" component={Dashboard} />
<Route exact path="/addProject" component={AddProject} />
</Routes>
</div>
</Router>
);
}
}
export default App;
I changed the import I added Routes to it. I wrapped the Route in Routes. But it keeps getting the same error.
I don't know if I should remove the Router, but if I do it receives the error: Uncaught TypeError: Cannot read properties of undefined (reading 'pathname')