I'm having issues configuring react-router-dom with nested routes loaded by sub-modules.
In my application I need to dynamically load modules that have their own nested routes. One of the requirement is to have the modules completely independent from the parent application. The loaded modules use relative routes to define their own navigation. The problem is that the relative navigation continue to append URL segments. I'm obviously missing something but I cannot figure it out.
The current behavior, that I would not expect, is that clicking multiple times to one of the nested navigation links will continue to append to the URL.
Initially, after clicking to Nested the URL is mydomain.com/nested. If I click the link for sub the ULR changes to mydomain.com/nested/sub. If I click again sub it becomes mydomain.com/nested/sub/sub or mydomain.com/nested/sub/other if I click to the other link.
I would expect the URL to remain mydomain.com/nested/sub or change to mydomain.com/nested/other.
I made a Codesandbox that shows the issue.
export default function App() {
return (
<BrowserRouter>
<div className='App'>
<h1>React Router Nested Routes</h1>
</div>
<Routes>
<Route element={<OuterLayout />}>
<Route
element={<Home />}
index
path='home'
/>
<Route
element={<Nested />}
path='nested/*'
/>
<Route
element={<Unknown />}
path='*'
/>
</Route>
</Routes>
</BrowserRouter>
)
}
function OuterLayout() {
return (
<div>
<ul>
<li>
<Link to='/home'>Home</Link>
</li>
<li>
<Link to='/nested'>Nested</Link>
</li>
</ul>
<Outlet />
</div>
)
}
function Home() {
return <section>Home</section>
}
function Unknown() {
return <section>Unknown</section>
}
function Nested() {
return (
<Routes>
<Route element={<NestedLayout />}>
<Route
element={<Sub />}
index
/>
<Route
element={<Sub />}
path='sub'
/>
<Route
element={<Other />}
path='other'
/>
</Route>
</Routes>
)
}
function NestedLayout() {
return (
<div>
<ul>
<li>
<Link to='sub'>Sub</Link>
</li>
<li>
<Link to='other'>Other</Link>
</li>
</ul>
<Outlet />
</div>
)
}
function Sub() {
return <section>Sub</section>
}
function Other() {
return <section>Other</section>
}