Closed devYuraKim closed 2 months ago
BrowserRouter
provides routing contextMoving the PageNav
component inside the BrowserRouter
component resolved the error.
function App() {
return (
<>
<p>constant element</p>
<BrowserRouter>
<PageNav />
<Routes>
<Route index element={<Homepage />} />
<Route path="/product" element={<Product />} />
<Route path="/pricing" element={<Pricing />} />
<Route path="*" element={<PageNotFound />} />
</Routes>
</BrowserRouter>
</>
);
}
PageNav
contains NavLink
component, which depends on the BrowserRouter
to provide routing context. Without being inside BrowserRouter
, it doesn't have access to important routing data like the current path or navigation functions, leading to errors.
In
App
component, thePageNav
component didn’t render and threw an error.App
componentFunny thing is, the same
PageNav
component worked fine inside theHomepage
component, even though both are using similar structures.Homepage
component