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

Error: Minified React error #321; #586

Open
R-AS opened this issue Mar 18, 2024 · 5 comments
Open

Error: Minified React error #321; #586

R-AS opened this issue Mar 18, 2024 · 5 comments

Comments

@R-AS
Copy link

R-AS commented Mar 18, 2024

I tried to use remote components in two sub-projects based on qiankun, and this error was reported when switching sub-applications. Is there any solution?
bug:

This is my configuration:
host1:

Federation({
  remotes: {
    remote: 'https://remote-dev.cn/static/remoteEntry.js',
  },
  shared: ['react', 'react-dom'],
}),

host1:

Federation({
  remotes: {
    remote: 'https://remote-dev.cn/static/remoteEntry.js',
  },
  shared: ['react', 'react-dom'],
}),

remote:

Federation({
  name: 'remote',
  filename: `remoteEntry_${version}.js`,
  exposes: {
    './Button': '/src/moduleFederationComponents/Button',
  },
  shared: ['react', 'react-dom',],
}),
@R-AS
Copy link
Author

R-AS commented Mar 18, 2024

error details:

Uncaught Error: Minified React error #321; visit https://reactjs.org/docs/error-decoder.html?invariant=321 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
    at p (__federation_shared_react-f847c8d7.js:12:3773)
    at u.useRef (__federation_shared_react-f847c8d7.js:12:5849)
    at withSelector_production_min.useSyncExternalStoreWithSelector (index-bd5e1e5b.js:264:361)
    at useStore (index-bd5e1e5b.js:264:1303)
    at de (index-bd5e1e5b.js:264:1513)
    at useMessage (index-bd5e1e5b.js:268:11357)
    at App (index-bd5e1e5b.js:820:16207)
    at Ch (react-dom.production.min.js:157:137)
    at ck (react-dom.production.min.js:267:460)
    at bk (react-dom.production.min.js:250:347)

@satyam-veris
Copy link

Hi, I am also facing this issue when using the same remote app in two different micro-frontends kept within a single spa root app. This occurs when I switch between these two apps, The first app loads perfectly but when I navigate to other app i get this same error, when switched back to first app, that app breaks as well.

Were you able to figure out anything?

@R-AS
Copy link
Author

R-AS commented May 9, 2024

Hi, I am also facing this issue when using the same remote app in two different micro-frontends kept within a single spa root app. This occurs when I switch between these two apps, The first app loads perfectly but when I navigate to other app i get this same error, when switched back to first app, that app breaks as well.

Were you able to figure out anything?

Hello, I looked at the vite-plugin-federation source code and found that when switching to the next application, the React instance of the previous application is still used.
In desperation, I adopted this plan.(The disadvantage is that the react version must be consistent)

  1. The remote and host transform react/react-dom imports into global variables,you can use rollup-plugin-external-globals,vite.config.ts just like:
import externalGlobals from "rollup-plugin-external-globals"

build: {
    rollupOptions: {
        external: ['react', 'react-dom'],
        plugins: [
            externalGlobals({
                'react': 'React',
		'react-dom': 'ReactDOM'
            }),
        ]
    }
}
  1. remote and host shared without react/react-dom
  2. remote and host use portal's React/ReactDOM, portal's code just like:
import React from 'react'
import ReactDOM from 'react-dom'

Object.defineProperty(window, 'React', {
    value: React,
    writable: false,
    configurable: false,
  })
  Object.defineProperty(window, 'ReactDOM', {
    value: ReactDOM,
    writable: false, 
    configurable: false, 
  })

@JessYan0913
Copy link

3. portal

How should I use the portal code?

@satyam-veris
Copy link

3. use portal's React/ReactDOM, portal's code just like:

Externalize react and react-dom from each and every micro frontends, and create a global scoped variable from the portal app/root/container

You can define the global variable in the entry file of your root app/container/portal app

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