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

React missing context when loaded by the "bridge" package #3538

Open
5 tasks done
intpp opened this issue Feb 20, 2025 · 3 comments
Open
5 tasks done

React missing context when loaded by the "bridge" package #3538

intpp opened this issue Feb 20, 2025 · 3 comments

Comments

@intpp
Copy link

intpp commented Feb 20, 2025

Describe the bug

It looks like there is an issue with context consumption when using the @module-federation/bridge-react package.

I have apps:

  • core - shares NameProvider component for all apps (NameProvider is a react context's provider)
  • host - entry point app, uses NameProvider and has remotes: core, remote-a, remote-b.
  • remote-a and remote-b - Apps that consume context from core

host/src/App.tsx

import HeroSection from 'core/HeroSection';
import { NameProvider } from 'core/Name';
import './App.css';
import BridgeRemoteA from './components/BridgeRemoteA';
import RemoteA from './components/RemoteA';
import RemoteB from './components/RemoteB';

const App = () => {
  return (
    <NameProvider name="Rsbuild">
      <HeroSection source="host" />
      <BridgeRemoteA />
      <RemoteA />
      <RemoteB />
    </NameProvider>
  );
};

export default App;

  • BridgeRemoteA - displays brige approach export/import
  • RemoteA - displays "React.lazy + Suspense" import
  • RemoteB - displays sync import

Result:
Image

Also, there are some issues with dynamic-remote-type-hints-plugin:

Image

Reproduction

https://github.com/intpp/rsbuild-module-federation-playground

Used Package Manager

pnpm

System Info

Binaries:
  Node: 20.3.0 - ~/.nvm/versions/node/v20.3.0/bin/node
  pnpm: 10.4.1 - ~/Library/pnpm/pnpm

Validations

@mbaric1
Copy link

mbaric1 commented Feb 24, 2025

I had same problem and it's not how it works. You would have to pass the context as props to bridge component and create new context. Context is held within the app tree of react itself.

Here is working example: https://github.com/mbaric1/mf-context-sharing

@intpp
Copy link
Author

intpp commented Feb 24, 2025

According to what I see, currently, the bridge component mounts to a separated dom element, and because of it, context doesn't work. But I need this proxy of react-router in my app. So I see a few solutions here:

  • Do not do it with separated node
  • Move the proxy of react-router functionality to a separate package to have the ability to use it independently

Am I right?

@ScriptedAlchemy
Copy link
Member

correct, you are mounting apps onto divs / nodes - like single spa, when using bridges - you cannot access memory singletons like context in this mode because they are essentially separate app trees

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

3 participants