module-federation / module-federation-examples

Implementation examples of module federation , by the creators of module federation
https://module-federation.io/
MIT License
5.66k stars 1.75k forks source link

Exposed component cannot be wrapped with Redux provider when remote is consumed as dynamic remotes #4282

Closed AshanPerera98 closed 1 month ago

AshanPerera98 commented 1 month ago

I have two react apps running on the following stack


When shell app tries to render the remote it gives the following errors in the console

Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app

AND

react.development.js:1650 Uncaught TypeError: Cannot read properties of null (reading 'useMemo')

When I remove the Provider and the Store config from the constructor it works fine.

This is the module-federation sample that I followed https://github.com/module-federation/module-federation-examples/blob/master/advanced-api/dynamic-remotes/app1/src/App.js

ScriptedAlchemy commented 1 month ago

Have you shared React and Redux, etc. as a singleton?

AshanPerera98 commented 1 month ago

Yes @ScriptedAlchemy both singleton: true and eager: true

ScriptedAlchemy commented 1 month ago

do not eager share it

ScriptedAlchemy commented 1 month ago

Send me your build configs too

ScriptedAlchemy commented 1 month ago

also use registerRemote, not init since you are not passing shared modules in this init, just adding more remotes to the existing instance im guessing.

AshanPerera98 commented 1 month ago

@ScriptedAlchemy here is a sample repo I created with the issue in the issue/dynamic-module branch issue dynamic module with redux

Can you check this repo

ScriptedAlchemy commented 1 month ago

Stop using eager: true for everything. None of the other apps share Redux in the federation configuration either.

The host/shell does not pass any shares in its initialization because everything is eagerly shared and dynamically loaded. The system likely tears down the share scope because the host already uses eager React before the share initialization can complete. So multiple versions are loaded since the system later chose a different version.

Do not use eager: true. You should also consider using rsbuild instead of CRA.

ScriptedAlchemy commented 1 month ago

You also are not using v2. Should be using module-federation/enhanced/webpack

Not what's built into webpack. That's v1

AshanPerera98 commented 1 month ago

@ScriptedAlchemy It worked with v2. Thanks for the support it means a lot.

AshanPerera98 commented 1 month ago

Closing the issue because the solution was found.

Problem: The Redux provider cannot be exposed to the shared module when consuming as a dynamic remote module federation. Solution: Upgrade module federation to v2 and remove eager consumptions from the config.

Below is the PR for the solution Solution

Thank You!