-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[v6] Nested routers #7375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Unfortunately, because of the way context is being used to allow for nested routes, we have to drop support for nested routers. It's not as simple as removing that one check, so we can't easily add it back. If you can make a PR that fully tests nested routers and resolves any issues with them, feel free. But right now it's not something we're considering doing ourselves. |
@timdorr Could you point to implementation details that prevent nesting router contexts? This is quite the surprising behavior since React context nesting should "just work". I understand problems with nesting for certain implementations (e.g. BrowserRouter). But BrowserRouter > MemoryRouter seems harmless to me (maybe even MemoryRouter > MemoryRouter). Would help alot to understand what we should test about nesting routers. |
There might be a dirty hack to achieve this feature: RRv6 exports the <UNSAFE_LocationContext.Provider value={ null }>
<MemoryRouter>
{ /* some routes not related to the outer router */ }
</MemoryRouter>
</UNSAFE_LocationContext.Provider> |
following - would love to see some implementation that supports this again in v6. |
yes, It seems like |
Of course, this would lead users to unexpected behaviors, and should be discouraged. <MemoryRouter UNSAFE_allowNesting /> (separate comment so people could vote on it separately) |
Has anyone figured out a workaround or alternative for this? We use |
@tylergraf I only use the When we were upgrading our projects, we had lots of components that SHOULD NOT update browser history, and were built originally with a Since we already had all these great Heres an example of a Example of a self contained modal within the You have a few options here...
Having one top level invariant(
!useInRouterContext(),
`You cannot render a <Router> inside another <Router>.` +
` You never need more than one.`
); The CONS: You need to know what router context your currently in, and for us that really wasn't an issue. If you are trying to change browser history within your sub router, you can export your browser router... and do something like this:
I am quite sad that theres no plan to add this back in, as i believe this feature is needed to create very complex and reusable UIs without re-inventing how to show modals, tabs, etc when |
@jamesmarrs I agree: removing that invariant() line makes it work perfectly; whichever Router context you're within currently is the one the React components use. This makes logical sense and should be possible with this package. Just add a note in the docs saying that these components will use whatever the closest parent RouterContext is. |
remix-run#7375 (comment) Signed-off-by: Christian Stewart <[email protected]>
@jamesmarrs I ended up forking react-router to accomplish this, wish it wasn't necessary, but it seems like no progress has been made on #9601 - https://github.com/aperturerobotics/remix-react-router |
remix-run#7375 (comment) Signed-off-by: Christian Stewart <[email protected]>
@paralin , do you've a working example using your forked repo? I used your forked package it still doesn't differentiate between parent nav and child nav. |
@tejasviSri you can nest Router in that version and since the router stuff uses RouterContext, whichever parent context is there is used. But feel free to open an issue there with your use case and I'll check. |
I think you've meant to mention @tylergraf? |
@teameh mentioned you instead of @tejasviSri - apologies |
No problem :D |
Hi, currently in
v6
it's not possible to have supported routers because of this check. In the past it was possible to have a<MemoryRouter />
inside of a<BrowserRouter />
. This is very useful if you have a section with multiple routes that can work standalone but can also work within a different page in a modal. In that case you can wrap it in a memory router and it the routing still works without navigating away from the current page.Would you consider a PR that lifted this restriction or provide some workaround for this?
The text was updated successfully, but these errors were encountered: