I am building a small react app, and I have implemented my routes like this:
import React, { lazy } from "react";
import ReactDOM from "react-dom/client";
import { BrowserRouter, Routes, Route } from "react-router-dom";
const App = lazy(() => import("./App"));
const Home = lazy(() => import("./Pages/Home"));
const Registro = lazy(() => import("./Pages/Dashboard"));
const Summary = lazy(() => import("./Pages/Summary"));
const Inventario = lazy(() => import("./Pages/Inventario"));
const Estadisticas = lazy(() => import("./Pages/Estadisticas"));
const Dashboard = lazy(() => import("./Pages/Dashboard"));
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<BrowserRouter>
<Routes>
<Route path="/" element={<App />}>
<Route index element={<Home />} />
<Route path="dashboard" element={<Dashboard />}>
<Route index element={<Summary />} />
<Route path="estadisticas" element={<Estadisticas />} />
<Route path="inventario" element={<Inventario />} />
</Route>
</Route>
</Routes>
</BrowserRouter>
</React.StrictMode>
);
As you can see I got some nested routes inside my Dashboard component, where I have set the Summary component to match by default using the index prop. My dashboard component is something like this:
import { Outlet, Link } from "react-router-dom";
export default function Dashboard() {
return (
<div>
<nav>
<ul>
<li><Link to="/">Summary</Link></li>
<li><Link to="estadisticas">Estadisticas</Link></li>
<li><Link to="inventario">Inventario</Link></li>
</ul>
</nav>
<Outlet/>
</div>
);
}
My problem comes when I try to access the Summary component from Link to="/" making use of the nav, instead of rendering the Summary component, it renders the Home component. I have also tried with Link to="dashboard", but the url ends up being something like dashboard/dashboard, which does not work. is there any way to access the Summary component from using Link to?