React应用在本地和GitHub页面上都显示404组件而不是索引页。

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

React Application Serving 404 Component Instead of Index on Both Local and GitHub Pages

问题

我正在开发一个React应用程序,并遇到一个问题,该应用程序显示404错误组件而不是主页组件。这在我的本地开发服务器上以及将应用程序部署到GitHub Pages时都会发生。

我的代码可在GitHub上找到:arch-studio-multipage-website存储库

以下是我在App.js中的路由设置:

import { createBrowserRouter, Route, createRoutesFromElements, RouterProvider } from "react-router-dom";
import RootLayout from "./layout/RootLayout";
import Home from "./pages/Home";
import NotFound from "./pages/NotFound";

const router = createBrowserRouter({
  basename: '/arch-studio-multipage-website',
  routes: createRoutesFromElements(
    <Route path="/" element={<RootLayout />}>
      <Route index element={<Home />} />
      <Route path="*" element={<NotFound />} />
    </Route>
  )
});

function App() {
  return <RouterProvider router={router} />;
}

export default App;
英文:

I am working on a React application and facing an issue where the application is showing the 404 error component instead of the index component. This is happening both on my local development server as well as when the application is deployed to GitHub Pages.

My code is available on GitHub: arch-studio-multipage-website repository.

Here is my routing setup in App.js:

import { createBrowserRouter, Route, createRoutesFromElements, RouterProvider } from "react-router-dom";
import RootLayout from "./layout/RootLayout";
import Home from "./pages/Home";
import NotFound from "./pages/NotFound";

const router = createBrowserRouter({
  basename: '/arch-studio-multipage-website',
  routes: createRoutesFromElements(
    <Route path="/" element={<RootLayout />}>
      <Route index element={<Home />} />
      <Route path="*" element={<NotFound />} />
    </Route>
  )
});

function App() {
  return <RouterProvider router={router} />;
}

export default App;


</details>


# 答案1
**得分**: 1

问题在于您错误地使用了 `createBrowserRouter`根据[文档][1]应该有两个参数 - 第一个参数应该是一个路由数组在您的情况下 `createRoutesFromElements` 返回),第二个参数应该是一个包含选项的对象在您的情况下是 `basename`)。

以下是修复后的 `router` 对象
```jsx
const router = createBrowserRouter(
  createRoutesFromElements(
    <Route path='/' element={<RootLayout />} >
      <Route index element={<Home />} />
      <Route path='*' element={<NotFound />} />
    </Route>
  ),
  {
    basename: '/arch-studio-multipage-website',
  }
);

为了避免将来出现此类错误,我建议使用 TypeScript - 在这种情况下,TypeScript 应该能够检测到参数的类型不正确并指导您解决问题。

此外,您的 package.json 中缺少 react-router-dom 作为依赖项 - 您可能在全局安装了它,而不是在项目内部安装。在此项目的目录中运行 npm install react-router-dom 以修复此问题。

英文:

the problem is that you're using createBrowserRouter incorrectly. According to the documentation, there should be two arguments - the first one should be an array of routes (in your case, returned by createRoutesFromElements and the second argument should be an object with options (in your case, basename).

Here is a fixed router object:

const router = createBrowserRouter(
  createRoutesFromElements(
    &lt;Route path=&#39;/&#39; element={&lt;RootLayout /&gt;}&gt;
      &lt;Route index element={&lt;Home /&gt;} /&gt;
      &lt;Route path=&#39;*&#39; element={&lt;NotFound /&gt;} /&gt;
    &lt;/Route&gt;
  ),
  {
    basename: &#39;/arch-studio-multipage-website&#39;,
  }
);

To avoid such errors in the future, I recommend using TypeScript - in this case, TypeScript should be able to detect that the types of the arguments are incorrect and point you to the solution.

Also, react-router-dom is missing from the dependencies in your package.json - you've probably installed is globally instead of inside the project. Run npm install react-router-dom inside this project's directory to fix that.

答案2

得分: 0

我克隆了你的存储库并注意到了一些不同的情况:

  1. 你需要通过 npm i react-router-domreact-router-dom 安装到项目中。
  2. 你将参数传递给 createBrowserRouter 的方式不正确。它的第一个参数应该是路由列表,第二个参数是选项。我成功地使它正常工作,通过将它更改为以下内容:
const router = createBrowserRouter(
  createRoutesFromElements(
    &lt;Route path=&quot;/&quot; element={&lt;RootLayout /&gt;}&gt;
      &lt;Route index element={&lt;Home /&gt;} /&gt;
      &lt;Route path=&quot;*&quot; element={&lt;NotFound /&gt;} /&gt;
    &lt;/Route&gt;
  ),
  {
    basename: &quot;/arch-studio-multipage-website&quot;,
  }
);
英文:

I cloned your repo and noticed a couple different things going on:

  1. You need to install react-router-dom to the project itself via npm i react-router-dom
  2. You're passing arguments to createBrowserRouter incorrectly. It takes a list of routes as the first parameter and options second. I was able to get it working correctly by changing it to this:
const router = createBrowserRouter(
  createRoutesFromElements(
    &lt;Route path=&quot;/&quot; element={&lt;RootLayout /&gt;}&gt;
      &lt;Route index element={&lt;Home /&gt;} /&gt;
      &lt;Route path=&quot;*&quot; element={&lt;NotFound /&gt;} /&gt;
    &lt;/Route&gt;
  ),
  {
    basename: &quot;/arch-studio-multipage-website&quot;,
  }
);

huangapple
  • 本文由 发表于 2023年7月3日 04:05:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600584.html
匿名

发表评论

匿名网友

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

确定