Skip to content
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

[v6] Allow nesting <Router> components #7890

Closed
philer-jambit opened this issue Jun 29, 2021 · 1 comment
Closed

[v6] Allow nesting <Router> components #7890

philer-jambit opened this issue Jun 29, 2021 · 1 comment

Comments

@philer-jambit
Copy link

philer-jambit commented Jun 29, 2021

Background
We're building a complex architecture with nested micro services, where multiple "Apps" live in the same React tree. We want to allow these Apps to benefit from independent routing. See description below for details.

Problem
This should work, except for one problem: The <Router> component does not like being nested. There is an explicit check on line 288 to prevent this, with the message:
You cannot render a <Router> inside another <Router>. You never need more than one.
Unless there's a better way to achieve our requirement, we do indeed need more than one.

Suggestion
As far as I can tell this check did not exist in v5 and is supposed to help developers avoid a common mistake. That is useful, but undermines our use case. My suggestion is to either remove the check or to add a prop that allows disabling it (something like <Router allowNestedRouter>) as an informed choice.
As far as I can tell there should be no technical limitation preventing this, as Router is just a context provider and nested context providers will just shadow their ancestors.

Details
Our attempted solution is based on a custom Router with a custom History sub class that can be injected for each App (based on this suggestion). The individual Apps' current pathnames are combined and appended to the page URL's hash via one "main" Router+History.

A URL could look something like this (extra spaces for readability): https://example.com/home #foo:/hello #bar:/settings

Here's what a corresponding React tree in our setup might look like:

<RootRouter> // this is mostly a copy of BrowserRouter and uses a BrowserHistory instance
    ... page level routing points to /home ...

    <SomeApp>
        <AppRouter name="foo"> // also similar to BrowserRouter, but uses a custom AppHistory instance
            ... app level routing, points to /hello ... 
        </AppRouter>
    </SomeApp>

    <SomeOtherApp>
        <AppRouter name="bar>
            ... app level routing, points to /settings ... 
        </AppRouter>
    </SomeOtherApp>

</RootRouter>

The custom AppHistory class overwrites methods like push and replace to transform path changes into hash changes for the <RootRouter>'s BrowserHistory

Please let me know if my suggestion is viable and if so, which option you prefer. I'd be happy to submit a pull request to help things along.
Thank you for your awesome work! 👍

@timdorr
Copy link
Member

timdorr commented Jun 29, 2021

Duplicate of #7375

@timdorr timdorr marked this as a duplicate of #7375 Jun 29, 2021
@timdorr timdorr closed this as completed Jun 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants