Bug
4.0.0
https://codepen.io/anon/pen/NgXNxE?
View "Subroute Home page." header
Address bar also changes
View "Subroute message." header
Don't follow to SubrouteIndex
Addressbar not handle route change
You shouldn't nest BrowserRouter ever. If you need an isolated Router, use MemoryRouter.
But how implement nested routing with handling it in address bar? Why my realization is bad?
You can't do that. The top level BrowserRouter is going to create a history instance that will listen to pushState events. There is no way to do what you're looking to do in any version of React Router.
Thx a lot!
Having a nested BrowserRouter (or equivalent) may be a common requirement in large applications that are separated in to modules.
In our case, this is solely so that:
I looked in to the issues described by @timdorr above and think I have a solution.
I created a NestedBrowserRouter component that uses the parent router's history but adds a basepath:
https://github.com/penx/nested-browser-router
I have put a demo usage here:
https://codesandbox.io/s/q4l9vo1m89
<BrowserRouter>
<React.Fragment>
<h1>Application</h1>
<Link to="/">Home</Link> | <Link to="/sub">Sub app</Link> |{" "}
<Link to="/sub/sub">Sub sub</Link>
<Switch>
<Route exact path="/" render={() => <h2>Home</h2>} />
<Route
path="/sub"
render={() => (
<NestedBrowserRouter basename="sub">
<>
<h1>Sub app</h1>
<Link to="/">Sub Home</Link> | <Link to="/sub">Sub Sub</Link>
<Switch>
<Route exact path="/" render={() => <h2>Sub Home</h2>} />
<Route exact path="/sub" render={() => <h2>Sub Sub</h2>} />
</Switch>
</>
</NestedBrowserRouter>
)}
/>
</Switch>
</React.Fragment>
</BrowserRouter>
Caveats: