I am new to React, and I cannot get my routes to work. I am following the tutorial in the documentation but somehow it does not work. The content inside the App component does not show up so I assume there is some kind of problem with BrowserRouter or Routes or my nesting but I really cannot figure out what's going on.
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter, Routes, Route } from "react-router-dom";
import App from './App';
import Products from './routes/products';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<BrowserRouter>
<Routes>
<Route path="/" element={<App />}>
<Route path="products" element={<Products />} />
</Route>
</Routes>
</BrowserRouter>
</React.StrictMode>
);
routes/app.js
import { Outlet, Link } from "react-router-dom";
export default function App() {
return (
<div>
<h1>Hello World</h1>
<nav>
<Link to="/products">Products</Link>
</nav>
<Outlet />
</div>
);
}
routes/products.jsx
export default function Products(){
return (
<div>
<h1>Products</h1>
</div>
);
}
P.S. I already saw some stackoverflow answers but still I could not see what I am getting wrong.
EDIT:
Thank you for the attention and support but after rewriting the code and playing around with it I found out that my App component does not render when I add the Link to="" component. Any Idea why this is happening?