question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic / Nested routes

See original GitHub issue

Hi there

In your examples shared-routing and shared-routes2 you showed how to use react-router-dom with Module Federation. However, you defined route config, where specified all route paths and their Components in one place. It’s not always the case and good only for TODO-size applications. In real world we use many Switch, Link, Route elements scattered everywhere in code. It is not possible to combine all the route definitions in one place.

I have problem using react-router-dom with Module Federation, because a component which I expose from MicroFrontend 1 has Switch and Link elements inside. The MicroFrontend 2, which consumes the remote component has BroswerRouter, wrapping the remote component, however it does not work. In JS runtime I get the next errors:

Uncaught Error: Invariant failed: You should not use <Link> outside a <Router> Uncaught Error: Invariant failed: You should not use <Switch> outside a <Router>

Links and switch elements from remote component of Microfrontend 1 are not inside BrowserRouter of Microfrontend 2. What is the solution?

RemoteComponent of Microfrontend 1

const RemoteComponent = () => {
  return (
    <>
      <ul>
        <li>
          <Link to="/path/path1">Path1</Link>
        </li>
        <li>
          <Link to="/path/path2">Path2</Link>
        </li>
      </ul>
      <Switch>
        <Route path="/path/path1">Path1</Route>
        <Route path="/path/path2">Path2</Route>
      </Switch>
    </>
  );
};

export default RemoteComponent;

webpack config of MicroFrontend1

new ModuleFederationPlugin({
      name: "MFE1",
      filename: "remoteEntry.js",
      exposes: {
        "./RemoteComponent": "./src/components/RemoteComponent/RemoteComponent",
      },
    })

App of MicroFrontend 2

const RemoteComponent = lazy(() => import("MFE1/RemoteComponent"));

function App() {
  return (
    <BrowserRouter>
      <ul>
        <Link to="/path">General path</Link>
      </ul>
      <Suspense fallback="...loading">
        <Switch>
          <Route path="/path">
            <RemoteComponent />
          </Route>
        </Switch>
      </Suspense>
    </BrowserRouter>
  );
}

webpack config of Microfrontend 2

new ModuleFederationPlugin({
      name: "MFE2",
      remotes: {
        MFE1: "MFE1@http://localhost:8080/remoteEntry.js",
      },
    }),

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
rvitrukcommented, Apr 14, 2021

I have already found the solution by myself !

I should have put react-router-dom in shared

This issue may be closed.

0reactions
dyukovladcommented, Apr 27, 2022

@GastonMGrecco no, we ourselves use 5.3

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create nested dynamic routes using React Router v6
Now we need to create a matching nested route in App Component. First it is Nested Route of /user/transactions, we can place it...
Read more >
Nested Dynamic Routes in React Router v6
Nested Dynamic Routes in React Router v6 🎯​​ If you look closely, you'll see that the URL is dynamically altered to reflect the...
Read more >
2 levels nested routing in nextjs - Stack Overflow
I want dynamic routing at two levels. E.g.: {siteroot}/dynamicPage; {siteroot}/dynamicUrlSection/dynamicPage. My folder structure is: / ...
Read more >
Routing: Introduction - Next.js
If you create a nested folder structure, files will automatically be ... Check out the Dynamic Routes documentation to learn more about how...
Read more >
Dynamic And Nested Routes In React Router v5
Dynamic routing happens when the app is rendering. An example of a dynamic route is the /:slug route, which matches anything that comes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found