React Routes not working with Module Federation

huangapple go评论129阅读模式
英文:

React Routes not working with Module Federation

问题

这里是根应用程序的路由。

这里是位于.packages/payments/scr/app目录中的Payments应用程序的路由。

问题是,“Payments”页面已呈现并正常工作,但“Payments”内部的所有路由都不起作用(这是我得到的情况,没有错误)。

我猜测“Payments”内部的路由只是未初始化,我不明白为什么(根据React v18文档制作的所有“bootstrap.jsx”文件都是正确的)。

在“Payments”应用程序中添加BrowserRouter会导致错误

无法在另一个<Router>内部呈现<Router>

也许我没有注意到某些细节,如果您需要任何其他设置详细信息,请告诉我。

英文:

So I'm using React v18, Router v6 and Module federation

this is root app routes

this is Payments app routes in .packages/payments/scr/app

the problem is that Payments page is rendered and working fine, but all routes inside Payments not working at all this is what i get, no errors

My guess is that routes inside Payments are just not initialized and I don't understand why.All bootstrap.jsx files are made accordingly by React v18 docs

Adding BrowserRouter inside Payments app is causing error

> You cannot render &lt;Router&gt; inside another &lt;Router&gt;

Maybe I don't see something, let me know if you need any additional setup details.

答案1

得分: 1

主要的App代码在为后代路由添加通配符&quot;*&quot;匹配器时是正确的。

&lt;Route
  path=&quot;/payments-kz/*&quot;
  element={&lt;PaymentsKzPage store={store} /&gt;}
/&gt;

问题出在PaymentsKzPage渲染后代路由上。嵌套的Routes组件会根据父路由构建其路由,例如&quot;/payments-kz&quot;。后代路由错误地在路径前添加了&quot;payments-kz&quot;路径段。

示例:

&lt;Route
  path=&quot;/payments-kz/catalog/:categoryId/providers/:subcategoryId&quot;
  element={&lt;Providers /&gt;}
/&gt;

实际路径解析为:

&quot;/payments-kz/payments-kz/catalog/:categoryId/providers/:subcategoryId&quot;

为解决这个问题,您应该从后代路由中删除&quot;/payments-kz&quot路径段。

&lt;Routes&gt;
  &lt;Route
    path=&quot;/catalog/:categoryId/providers/:subcategoryId&quot;
    element={&lt;Providers /&gt;}
  /&gt;
  &lt;Route path=&quot;/catalog/:id/:category&quot; element={&lt;Subcategories /&gt;} /&gt;
  &lt;Route path=&quot;/catalog/:id&quot; element={&lt;Subcategories /&gt;} /&gt;
  &lt;Route path=&quot;/providers/:subcategoryId&quot; element={&lt;Providers /&gt;} /&gt;
  ....等等....
&lt;/Routes&gt;
英文:

The main App code is correct when it appends a wildcard &quot;*&quot; matcher to the routes for the descendent routes to be matched.

&lt;Route
  path=&quot;/payments-kz/*&quot;
  element={&lt;PaymentsKzPage store={store} /&gt;}
/&gt;

The issue is in the PaymentsKzPage rendering the descendent routes. The nested Routes component builds its routes relative to the parent route, e.g. &quot;/payments-kz&quot;. The descendent routes are incorrectly prepending a &quot;payments-kz&quot; path segment.

Example:

&lt;Route
  path=&quot;/payments-kz/catalog/:categoryId/providers/:subcategoryId&quot;
  element={&lt;Providers /&gt;}
/&gt;

The path actually resolves to:

&quot;/payments-kz/payments-kz/catalog/:categoryId/providers/:subcategoryId&quot;

To resolve the issue you should remove the &quot;/payments-kz&quot; segment from the descendent routes.

&lt;Routes&gt;
  &lt;Route
    path=&quot;/catalog/:categoryId/providers/:subcategoryId&quot;
    element={&lt;Providers /&gt;}
  /&gt;
  &lt;Route path=&quot;/catalog/:id/:category&quot; element={&lt;Subcategories /&gt;} /&gt;
  &lt;Route path=&quot;/catalog/:id&quot; element={&lt;Subcategories /&gt;} /&gt;
  &lt;Route path=&quot;/providers/:subcategoryId&quot; element={&lt;Providers /&gt;} /&gt;
  .... etc ...
&lt;/Routes&gt;

huangapple
  • 本文由 发表于 2023年3月15日 19:18:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定