So I was following the React Crash Course on YouTube for Beginners from Academind. I was following everything along and everything was good until I came to the Routing part. I followed every step, everything just perfectly (for the routing part) but after refreshing the page the following error occurs:
A
<Route>
is only ever to be used as the child of<Routes>
element, never rendered directly. Please wrap your<Route>
in a<Routes>
.
Aaand I did it, I wrapped my Route in Routes :
import { Route } from 'react-router-dom';
import AllMeetupsPage from './Pages/AllMeetups';
import NewMeetupsPage from './Pages/NewMeetups';
import FavoritesPage from './Pages/Favorites';
function App() {
return (
<div>
<Routes>
<Route path='/'>
<AllMeetupsPage />
</Route>
<Route path='/new-meets'>
<NewMeetupsPage />
</Route>
<Route path='/favs'>
<FavoritesPage />
</Route>
</Routes>
</div>
);
}
export default App;
and then I get this:
'Routes' is not defined react/jsx-no-undef
then I :
Tried to import Routes from react-router-dom - No success;
Tried to import Routes from react-router - No success;
Tried to import Routes also in different components - No success;
Trust me I tried every different scenario for Routes but couldnt achieve anything different. I
Googled, researched and couldnt find the solution for this problem.. Now Im desperate and stuck here and I cant continue my React learning journey if I dont fix this...
CodePudding user response:
Try import { BrowserRouter as Routes, Route } from react-router-dom
Docs: https://v5.reactrouter.com/web/guides/quick-start
CodePudding user response:
in place of Routes
you should use Switch
nested routes
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
function App() {
return (
<div>
<Router>
<Switch>
<Route path='/'>
<AllMeetupsPage />
</Route>
<Route path='/new-meets'>
<NewMeetupsPage />
</Route>
<Route path='/favs'>
<FavoritesPage />
</Route>
</Switch>
</Router>
</div>
);
}