Navigate out of a nested Memory router
I have a nested MemoryRouter inside a BrowserRouter. Question is, how do I navigate out of a MemoryRouter?
Basically, I have a link in a page in my MemoryRouter that takes the user to a route listed in my BrowserRouter, yet this simply leads to an empty page as it thinks the page is inside my MemoryRouter when it isn't.
I have implemented a solution where I pass a higher-level callback into my MemoryRouter that executes when I call this in the MemoryRouter, yet this seems a bit laborious to say the least.
Is there something I'm missing from the docs or is this simply a limitation that I will have to work around as I want to have specific routes that do not change the URL.
You have to create your own history instance and use Router. You'll have to get access to that history instance to be able to change the route.
Thanks for the reply.
I've had a look at this solution but can't seem to get it working. Can you provide an example as there just does not seem to be any clear examples of how to do this without providing a nested callback which seems a bit hacky?
Something like this:
const history = createBrowserHistory()
window.browserHistory = history
ReactDOM.render(<Router history={history}> //...
// elsewhere, underneath a <MemoryRouter>
<a href="#" onClick={e => { e.preventDefault(); window.browserHistory.push('/foo') }} />
Thanks for the reply. I've just managed to figure it out also after rethinking how I can achieve this and came up with a very similar solution whereby I simply pass the top-level history instance as a prop to my MemoryRouter component, allowing all nested routes access to this instance of 'appHistory' to be able to navigate out of the MemoryRouter, which works exactly the way I want it to. Thanks for the fast reply either way though as it turns out I was on track after looking into this more : )