Hi am trying to update project using react router to v6. I get the basics but am struggling with relative links.
We have a page that renders reference documentation for a given item by 'id'. The documentation can have links to other 'sibling' material, using sibling id. In other words the user can navigate around the docs without ever leaving the same basic route (parameterised by 'id').
I have made a little repro on codesandbox, with essential code below.
import React from "react";
import {
BrowserRouter,
Routes,
Route,
Link,
useParams
} from "react-router-dom";
import "./styles.css";
function GenericPage() {
const { id } = useParams();
return (
<div className="page">
<p>Document id: {id}</p>
<div>
Links from within page don't work:
<Link to="./foo" className="link">
Foo
</Link>
<Link to="./bar" className="link">
Bar
</Link>
</div>
</div>
);
}
export default function App() {
return (
<BrowserRouter>
<div>
Working top nav:
<Link to="docs/generic/foo" className="link">
Foo
</Link>
<Link to="docs/generic/bar" className="link">
Bar
</Link>
</div>
<Routes>
<Route path="/docs/generic/:id" element={<GenericPage />} />
</Routes>
</BrowserRouter>
);
}
Have tried using ../{id}, etc to no avail.
Maybe this is by design but it seems a bit odd to disallow a link to sibling pages using a simple relative name. All pretty normal in regular web dev.