I was trying to hit route "/" to render Home component. The component is getting rendered but on the console I am getting:
react_devtools_backend.js:3973 No routes matched location "/"
at Routes (http://localhost:3000/static/js/0.chunk.js:39008:24)
at Router (http://localhost:3000/static/js/0.chunk.js:38933:30)
at BrowserRouter (http://localhost:3000/static/js/0.chunk.js:38367:23)
at App
This is my App.js file:
import React from "react";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import GithubState from "./context/github/GithubState";
import Navbar from "./components/layout/Navbar";
import Home from "./components/pages/Home";
import User from "./components/users/User";
import About from "./components/pages/About";
import "./App.css";
const App = () => {
return (
<>
<Router>
<Navbar />
<GithubState>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/user/:username" element={<User />} />
</Routes>
</GithubState>
<Routes>
<Route path="/about" element={<About />} />
</Routes>
</Router>
</>
);
};
export default App;
Kindly help me out here.
The issue is because you are rendering two Routes
components and the second doesn't have a route path matching the current path "/"
.
Merge your Routes
components so you render a route with path "/"
. Creating a layout route to render the GithubState
provider will help a lot here.
import { Outlet } from 'react-router-dom';
const GithubStateLayout = () => (
<GithubState>
<Outlet /> // <-- nested routes render out here
</GithubState>
);
...
const App = () => {
return (
<>
<Router>
<Navbar />
<Route element={<GithubStateLayout />} >
<Route path="/" element={<Home />} />
<Route path="/user/:username" element={<User />} />
</Route>
<Route path="/about" element={<About />} />
</Router>
</>
);
};