0

一个简单的 POC,我们有一个 react 应用程序host_ui作为主机和material_components远程。material_ui公开一个组件NavBar

import React from 'react';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import Button from '@material-ui/core/Button';
import IconButton from '@material-ui/core/IconButton';
import MenuIcon from '@material-ui/icons/Menu';

const NavBar = () => {

  return (
    <AppBar position="static">
      <Toolbar>
        <IconButton edge="start" color="inherit" aria-label="menu">
          <MenuIcon />
        </IconButton>
        <Typography variant="h6">
          News
          </Typography>
        <Button color="inherit">Login</Button>
      </Toolbar>
    </AppBar>
  );
}

export default NavBar;

在主机中导入它会引发错误:

Uncaught Error: 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
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
    at resolveDispatcher (react.development.js:1476)
    at Object.useContext (react.development.js:1484)
    at useTheme (useTheme.js:10)
    at useStyles (makeStyles.js:237)
    at WithStyles(ForwardRef(AppBar)) (withStyles.js:68)
    at renderWithHooks (react-dom.development.js:14985)
    at updateForwardRef (react-dom.development.js:17044)
    at beginWork (react-dom.development.js:19098)
    at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)

react-dom.development.js:20085 The above error occurred in the <WithStyles(ForwardRef(AppBar))> component:

    at WithStyles(ForwardRef(AppBar)) (webpack://material_components/./node_modules/@material-ui/styles/esm/withStyles/withStyles.js?:61:31)
    at NavBar
    at Suspense
    at div
    at App

Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.

Uncaught Error: A cross-origin error was thrown. React doesn't have access to the actual error object in development. See https://reactjs.org/link/crossorigin-error for more information.
    at Object.invokeGuardedCallbackDev (react-dom.development.js:4005)
    at invokeGuardedCallback (react-dom.development.js:4056)
    at beginWork$1 (react-dom.development.js:23959)
    at performUnitOfWork (react-dom.development.js:22771)
    at workLoopSync (react-dom.development.js:22702)
    at renderRootSync (react-dom.development.js:22665)
    at performSyncWorkOnRoot (react-dom.development.js:22288)
    at eval (react-dom.development.js:11327)
    at unstable_runWithPriority (scheduler.development.js:646)
    at runWithPriority$1 (react-dom.development.js:11276)

虽然如果我们只使用 NavBar 组件中的 html 效果很好。material-ui在模块联合中使用是否需要做一些额外的事情。

4

2 回答 2

0

以这种方式为您的导航组件配置模块联合:

new ModuleFederationPlugin({
  name: "NavApp",
  filename: 'remoteEntry.js',
  exposes:{
    './NavComponent':'./src/{root of the nav project}'
  },
  shared: {
    ...deps,
    react: {
      singleton: true,
      requiredVersion: deps.react,
    },
    "react-dom": {
      singleton: true,
      requiredVersion: deps["react-dom"],
    },
  },
})

以这种方式为使用 Nav 的应用程序配置模块联合:

`new ModuleFederationPlugin({
      name: "Appliaction",
      remotes: {
        NavApp: 'NavApp@http://localhost:{port that is running your Nav 
        application}/remoteEntry.js',    
      },
      shared: {
        ...deps,
        react: {
          singleton: true,
          requiredVersion: deps.react,
        },
        "react-dom": {
          singleton: true,
          requiredVersion: deps["react-dom"],
        },
      },`

如下配置您的导航包:

  "dependencies": {
    "@babel/core": "^7.13.14",
    "@babel/preset-react": "^7.13.13",
    "add": "^2.0.6",
    "babel-loader": "^8.2.2",
    "css-loader": "^5.2.0",
    "html-webpack-plugin": "^5.3.1",
    "react-router": "^5.2.0",
    "react-router-dom": "^5.2.0",
    "style-loader": "^2.0.0",
    "webpack": "^5.30.0",
    "webpack-cli": "^4.6.0",
    "webpack-dev-server": "^3.11.2",
    "yarn": "^1.22.10",
    "@emotion/react": "^11.1.5",
    "@emotion/styled": "^11.1.5",
    "@material-ui/core": "^5.0.0-alpha.28",
    "@material-ui/icons": "^5.0.0-alpha.28",
    "@material-ui/styles": "^4.10.0",
    "@material-ui/system": "^4.11.3",
    "react": "^16.13.0",
    "react-dom": "^16.13.0",
    "serve": "^11.3.0"
  }

不要更改react或react-dom的版本,如果你有其他依赖,也添加

像这样配置主应用程序的包(正在使用 Nav):

   "dependencies": {
    "@babel/core": "^7.13.14",
    "@babel/preset-react": "^7.13.13",
    "babel-loader": "^8.2.2",
    "css-loader": "^5.2.0",
    "html-webpack-plugin": "^5.3.1",
    "react-router": "^5.2.0",
    "react-router-dom": "^5.2.0",
    "style-loader": "^2.0.0",
    "webpack": "^5.30.0",
    "webpack-cli": "^4.6.0",
    "webpack-dev-server": "^3.11.2",
    "@material-ui/core": "^4.9.5",
    "@material-ui/icons": "^4.9.1",
    "copy-webpack-plugin": "^5.1.1",
    "react": "^16.13.0",
    "react-dom": "^16.13.0",
    "serve": "^11.3.0"
  }

不要更改react或react-dom的版本,如果你有其他依赖,也添加

从两个应用程序中删除 node_modules 文件夹并删除文件 yarn.lock 或 package-lock.json。从 Yarn 或 Npm 再次在应用程序中安装(以安装新的依赖项)。在这些更改之后,一切都应该正常工作。

于 2021-04-06T19:04:52.230 回答
0

我也有这个问题,当APP1APP2React=17.0.0与. 基本上,我有一个特殊的配置来处理这种情况并下载一个新的 React 副本,但由于某种原因,它不能与.React=17.0.2MUI

仅当我为两个应用程序设置相同版本的 react 并在 ModuleFedration 设置中为两个应用程序设置该配置时,它才对我有用:

  shared: {
    react: {
    singleton: true,
    requiredVersion: deps["react"],
    },
    "react-dom": {
       singleton: true,
       requiredVersion: deps["react-dom"],
    }
  }

我在 MUI github paga 中打开了一个问题:https ://github.com/mui-org/material-ui/issues/29038#issuecomment-943317668

于 2021-10-13T15:54:46.823 回答