一页上的多个React Micro前端
我们正在使用 nx 生成反应应用程序,然后我们将其用作微正面端。 以前,它们都被限制在分开的页面上,并完美无缺。
最近,我们添加了一个应用程序,该应用程序旨在包含在所有页面上(使用layout.cshtml),这会导致问题。 当在同一页面上渲染两个应用程序时,会发生两件事:
- 第一个应用程序渲染,但没有事件(单击)触发,使其无法使用。
- 第二个应用程序根本不会渲染。
在部署环境中的浏览器控制台中没有显示错误。
在尝试重现问题时,仅当在页面上多次包含运行时。
无效的挂钩呼叫。钩子只能在功能组件的主体内部调用。这可能是出于以下原因之一:1。您可能有不匹配版本的React和渲染器(例如React Dom)2。您可能会违反钩子的规则3。同一应用参见有关如何调试和解决此问题的提示。
这使我相信这是我们的问题,包括页面上多次反应。 我已经开始研究为应用程序添加对同行依赖性的反应。由于我们一直在package.json中的主要依赖项中加入了反应。但是,由于应用程序是完全分开的,因此我无法弄清楚它应该如何工作。
有没有办法做这项工作?我想象以某种方式将所有常见的依赖项添加到一个文件中,并在每个页面上都包含它,而不是将它们全部包含在每个应用程序中。
html:
...
<div id="app1"></div>
<script src="/dist/apps/app1/runtime.js" type="text/javascript"/>
<script src="/dist/apps/app1/polyfills.esm.js" type="text/javascript"/>
<script src="/dist/apps/app1/main.esm.js" type="text/javascript"/>
...
<div id="app2"></div>
<script src="/dist/apps/app2/runtime.js" type="text/javascript"/>
<script src="/dist/apps/app2/polyfills.esm.js" type="text/javascript"/>
<script src="/dist/apps/app2/main.esm.js" type="text/javascript"/>
...
We're using nx to generate react applications, which we then use as micro front ends.
Previously they have all been confined to separate pages and have worked flawlessly.
Recently we've added an application that is meant to be included on all pages (using layout.cshtml), and this causes issues.
When two applications are to be rendered on the same page, two things happen:
- The first application renders, but no events (click etc.) trigger, making it unusable.
- The second application does not get rendered at all.
No errors are shown in the browser console in the deployed environment.
When trying to reproduce the problem, the following error is thrown only if runtime.js is included more than once on a page:
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.
This leads me to believe it's an issue with us including react multiple times on the page.
I've started looking into adding react to peer dependencies for the applications. Since we have been including react in the main dependencies in package.json. However, I cannot figure out how it's supposed to work in our case, since the applications are built completely separate from one another.
Is there a way to make this work? I am imagining somehow adding all common dependencies to a single file and including it on every page, instead of including them all in every application.
html:
...
<div id="app1"></div>
<script src="/dist/apps/app1/runtime.js" type="text/javascript"/>
<script src="/dist/apps/app1/polyfills.esm.js" type="text/javascript"/>
<script src="/dist/apps/app1/main.esm.js" type="text/javascript"/>
...
<div id="app2"></div>
<script src="/dist/apps/app2/runtime.js" type="text/javascript"/>
<script src="/dist/apps/app2/polyfills.esm.js" type="text/javascript"/>
<script src="/dist/apps/app2/main.esm.js" type="text/javascript"/>
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,我在发布后不久找到了解决方案。
此处详细介绍了该问题:
tl; dr:在每个React应用中全球添加的WebPackJSonp对象,需要具有一个唯一的名称才能工作。
因此,我必须执行以下操作:
。 workspace.json代码:
webpack.extra.js:
Of course I found the solution shortly after posting.
The issue is detailed here: link
tl;dr: the webpackJsonp object, which is added globally in each react app, needs to have a unique name for this to work.
So i had to do the following:
workspace.json snippet:
webpack.extra.js: