Using module federation with emotion/context
See original GitHub issueSetup
I’ve used the Next.js sidecar example and expanded to add a ThemeProvider from an internal component library that uses Emotion for styling.
Container Application
// _app.tsx
import Head from "next/head";
import { theme, Global, ThemeProvider } from '@internal/component-library';
export default function MyApp({ Component, pageProps }) {
return (
<ThemeProvider theme={theme}>
<Head>
<Global />
<script src="http://localhost:8081/remoteEntry.js" />
</Head>
<Component {...pageProps} />
</ThemeProvider>
);
}
// index.tsx
import { lazy, Suspense } from "react";
import { dependencies } from "../../package.json";
const RemoteComponent = ({ scope, module, ...props }) => {
if (!global[scope]) {
return null;
}
global[scope].init({
react: {
[dependencies.react]: {
get: () => Promise.resolve().then(() => () => require("react")),
},
},
});
const Component = lazy(() =>
global[scope].get(module).then((factory) => factory())
);
return (
<Suspense fallback={null}>
<Component {...props} />
</Suspense>
);
};
export default function Home() {
return (
<div>
<RemoteComponent scope="searchTeam" module="./ProductDetailsCard" propertyBedrooms="3" propertyId="12324" />
</div>
);
}
Remote module
// ProductDetailsCard.tsx
import React, { Component } from "react";
import { CardComponent } from '@internal/component-library';
const ProductDetailsCard = () => (
<CardComponent
isPremium={true}
/>
)
export default ProductDetailsCard;
Error
The above error occurred in the <Context.Consumer> component:
in Styled(div) (created by CardComponent)
in CardComponent (created by Context.Consumer)
in Styled(CardComponent) (created by ProductDetailsCard)
in ProductDetailsCard (at pages/index.tsx:23)
in Suspense (at pages/index.tsx:22)
in RemoteComponent (at pages/index.tsx:31)
in div (at pages/index.tsx:30)
in Home (at _app.tsx:12)
in ThemeProvider (at _app.tsx:6)
in MyApp
in ErrorBoundary (created by ReactDevOverlay)
in ReactDevOverlay (created by Container)
in Container (created by AppContainer)
in AppContainer
in Root
React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary.
Things tried
- Setting remote @internal/component-library to eager and to singleton (it would be good to find documentation on what these are doing)
- Removing remote dependencies (made no difference)
Any help would be very appreciated !
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Introduction to Micro Frontends with Module Federation, React ...
In this article we will set up Micro Frontends with Module Federation, React and Typescript.
Read more >State Management for Module Federation Four Ways - YouTube
... state management approaches you can use in your Module Federation architecture. ... Including Zustand, Redux, Context and prop drilling.
Read more >A Module Federation Example for React - Level Up Coding
Module federation allows a JavaScript application to dynamically load code from another application — in the process, sharing dependencies, if ...
Read more >Module Federation Architecture - Rangle.io
Module Federation —Federated Application Architectures ... There's been a lot of excitement in the community about the upcoming module federation ...
Read more >Micro Frontends Just Got Better With Module Federation
The example here is pretty simple. There is a remote app that exposes a component called Greet as a federated module, using exposes...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
side car was recently fixed, im going through all demo repos today to check they work after the final release of v5
You need to share more then react. Like emotion and those providers. Otherwise two context providers are loaded, not one. You also will want to share react dom too