React Hash Router v6 with basename

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

React Hash Router v6 with basename

问题

以下是代码部分的翻译:

const router = createHashRouter([
   {
      id: 'root',
      path: '/',
      element: <App />,
      errorElement: <ErrorPage />,
      loader: filtersLoader(queryClient),
      children: [
         { path: '/', element: <Navigate to="/dashboard/performance" /> },
         {
            path: '/dashboard',
            id: 'dashboard',
            errorElement: <ErrorPage />,
            loader: dataLoader(queryClient),
            element: (
               <>
                  <Header />
                  <Outlet />
               </>
            ),
            children: [
               { index: true, element: <Index /> }
            ]
         },
         {
            path: 'info',
            element: <Info />
         }
      ]
   }
], {
   basename: '/sub/route'
});

更新/解决方案如下:

问题是由于混合使用 HashRouter 和 basename 导致的。
在 createHashRouter 中去掉 basename 可以解决这个问题。

英文:

I need to use a hash router with a basename. I did this with the createHashRouter (React Router 6.8), with the basename property to render the app in the sub-route. When I open /sub/route in the browser I get the empty page though, only css background is visible.

All of the files and assets are shipped to the app, but the JS is not executed and the root div stays empty.

The code of the Router:

const router = createHashRouter([
   {
      id: &#39;root&#39;,
      path: &#39;/&#39;,
      element: &lt;App /&gt;,
      errorElement: &lt;ErrorPage /&gt;,
      loader: filtersLoader(queryClient),
      children: [
         { path: &#39;/&#39;, element: &lt;Navigate to=&quot;/dashboard/performance&quot; /&gt; },
         {
            path: &#39;/dashboard&#39;,
            id: &#39;dashboard&#39;,
            errorElement: &lt;ErrorPage /&gt;,
            loader: dataLoader(queryClient),
            element: (
               &lt;&gt;
                  &lt;Header /&gt;
                  &lt;Outlet /&gt;
               &lt;/&gt;
            ),
            children: [
               { index: true, element: &lt;Index /&gt; }
            ]
         },
         {
            path: &#39;info&#39;,
            element: &lt;Info /&gt;
         }
      ]
   }
], {
   basename: &#39;/sub/route&#39;
});

Update/solution

the issue was caused by mixing HashRouter with basename.
Getting rid of the basename in createHashRouter solved the issue.

答案1

得分: 1

HashRouter基本上已经可以在部署/提供React应用的任何目录中正常工作,指定basename在这里并不像对于BrowserRouter那样有太多意义,因为BrowserRouter需要它作为已解析路径的一部分。

&lt;Router basename&gt;属性可用于使应用中的所有路由和链接相对于URL路径名的“基本”部分。这在使用React Router渲染较大应用的一部分或应用具有多个入口点时很有用。基本名称不区分大小写。

HashRouter使用URL哈希进行路由,所以,例如,如果您添加了basename="/sub/route",那么您正在指定所有路由和链接相对于它进行操作,从其渲染的位置。如果应用程序托管/提供自“https://www.example.com/subA/subB”,那么您实际上是在说“https://www.example.com/subA/subB/#/sub/route”是应用程序运行的位置,浏览器需要导航到此URL以开始正确的路由。

如果"/sub/route"已经是应用程序托管/提供的位置,那么如果要保持路由/链接相对于"/"路径的默认行为在"https://www.example.com/sub/route/#/"上,就不需要设置basename

从路由配置中删除basename属性。

const router = createHashRouter([
   {
      id: 'root',
      path: '/',
      element: <App />,
      errorElement: <ErrorPage />,
      loader: filtersLoader(queryClient),
      children: [
         { path: '/', element: <Navigate to="/dashboard/performance" /> },
         {
            path: '/dashboard',
            id: 'dashboard',
            errorElement: <ErrorPage />,
            loader: dataLoader(queryClient),
            element: (
               <>
                  <Header />
                  <Outlet />
               </>
            ),
            children: [
               { index: true, element: <Index /> }
            ]
         },
         {
            path: 'info',
            element: <Info />
         }
      ]
   }
]);
英文:

The HashRouter will basically already just work from whatever directory you deploy/serve the React app from, specifying a basename doesn't make as much since here as it does for the BrowserRouter where the routes/links need it as part of their resolved paths.

> The &lt;Router basename&gt; prop may be used to make all routes and links
> in your app relative to a "base" portion of the URL pathname that they
> all share. This is useful when rendering only a portion of a larger
> app with React Router or when your app has multiple entry points.
> Basenames are not case-sensitive.

The HashRouter uses the URL hash for routing, so for example if you add that one has a basename=&quot;/sub/route&quot; then you are specifying that all routes and links operate relative to it where it's rendered. If the app is hosted/served from &quot;https://www.example.com/subA/subB&quot; then you are effectively saying &quot;https://www.example.com/subA/subB/#/sub/route&quot; is where the app is running and the browser will need to be navigated to this URL to start the proper routing.

If &quot;/sub/route&quot; is already where the app is hosted/served from then there's nothing to set as a basename if you want to keep the default behavior of routing/linking relative to a &quot;/&quot; path on &quot;https://www.example.com/sub/route/#/&quot;.

Remove the basename property from the routing configuration.

const router = createHashRouter([
   {
      id: &#39;root&#39;,
      path: &#39;/&#39;,
      element: &lt;App /&gt;,
      errorElement: &lt;ErrorPage /&gt;,
      loader: filtersLoader(queryClient),
      children: [
         { path: &#39;/&#39;, element: &lt;Navigate to=&quot;/dashboard/performance&quot; /&gt; },
         {
            path: &#39;/dashboard&#39;,
            id: &#39;dashboard&#39;,
            errorElement: &lt;ErrorPage /&gt;,
            loader: dataLoader(queryClient),
            element: (
               &lt;&gt;
                  &lt;Header /&gt;
                  &lt;Outlet /&gt;
               &lt;/&gt;
            ),
            children: [
               { index: true, element: &lt;Index /&gt; }
            ]
         },
         {
            path: &#39;info&#39;,
            element: &lt;Info /&gt;
         }
      ]
   }
]);

huangapple
  • 本文由 发表于 2023年3月8日 17:59:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671591.html
匿名

发表评论

匿名网友

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

确定