React HashRouter 在 GitHub Pages 上不渲染任何内容。

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

React HashRouter doesn't render anything on GitHub Pages

问题

HashRouter在GitHub页面上没有渲染任何内容,控制台也没有显示任何错误。只有在本地渲染时,如果在应用名称之前添加一个#,所以我不得不输入localhost:3000/chat-app,而是输入localhost:3000/#/chat-app。

App.js

import SignIn from './Components/SignIn';
import SignUp from './Components/SignUp';
import Home from './Components/Home';
import UserProfile from './Components/UserProfile';
import { HashRouter, Routes, Route } from 'react-router-dom';

function App() {
  return (
    <HashRouter>
      <Routes>
        <Route path="/chat-app" exact element={<Home />} />
        <Route path="/chat-app/signIn" exact element={<SignIn />} />
        <Route path="/chat-app/signUp" exact element={<SignUp />} />
        <Route path="/chat-app/userProfile" exact element={<UserProfile />} />
      </Routes>
    </HashRouter>
  );
}

export default App;

package.json:

{
  "homepage": "https://melosshabi.github.io/chat-app",
  "name": "chat-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "firebase": "^9.16.0",
    "nanoid": "^4.0.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.8.0",
    "react-scripts": "5.0.1",
    "universal-cookie": "^4.0.4",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "gh-pages": "^5.0.0"
  }
}

我已经尝试了一些在线找到的修复方法,但没有一个有效。如果您需要仓库链接,这里是:https://github.com/melosshabi/chat-app

我尝试了将导入从:import { HashRouter as Router, Routes, Route } from 'react-router-dom' 更改为:import { HashRouter, Routes, Route } from 'react-router-dom'

我还尝试更改:<Route path="/chat-app/home" element={<Home />} /> 为:<Route path="/chat-app" exact element={<Home />} />

英文:

HashRouter doesn't render anything on GitHub pages and the console doesn't show any errors either. Also it renders locally only if I put a # before the app name so instead of typing: localhost:3000/chat-app I have to type localhost:3000/#/chat-app.

App.js

import SignIn from &#39;./Components/SignIn&#39;;
import SignUp from &#39;./Components/SignUp&#39;;
import Home from &#39;./Components/Home&#39;;
import UserProfile from &#39;./Components/UserProfile&#39;;
import { HashRouter, Routes, Route } from &#39;react-router-dom&#39;

function App() {
  return (
    &lt;HashRouter&gt;
      &lt;Routes&gt;
        &lt;Route path=&quot;/chat-app&quot; exact element={&lt;Home /&gt;} /&gt;
        &lt;Route path=&quot;/chat-app/signIn&quot; exact element={&lt;SignIn /&gt;} /&gt;
        &lt;Route path=&quot;/chat-app/signUp&quot; exact element={&lt;SignUp /&gt;} /&gt;
        &lt;Route path=&quot;/chat-app/userProfile&quot; exact element={&lt;UserProfile /&gt;} /&gt;
      &lt;/Routes&gt;
    &lt;/HashRouter&gt;
  );
}

export default App;

package.json:

{
  &quot;homepage&quot;: &quot;https://melosshabi.github.io/chat-app&quot;,
  &quot;name&quot;: &quot;chat-app&quot;,
  &quot;version&quot;: &quot;0.1.0&quot;,
  &quot;private&quot;: true,
  &quot;dependencies&quot;: {
    &quot;@testing-library/jest-dom&quot;: &quot;^5.16.5&quot;,
    &quot;@testing-library/react&quot;: &quot;^13.4.0&quot;,
    &quot;@testing-library/user-event&quot;: &quot;^13.5.0&quot;,
    &quot;firebase&quot;: &quot;^9.16.0&quot;,
    &quot;nanoid&quot;: &quot;^4.0.1&quot;,
    &quot;react&quot;: &quot;^18.2.0&quot;,
    &quot;react-dom&quot;: &quot;^18.2.0&quot;,
    &quot;react-router-dom&quot;: &quot;^6.8.0&quot;,
    &quot;react-scripts&quot;: &quot;5.0.1&quot;,
    &quot;universal-cookie&quot;: &quot;^4.0.4&quot;,
    &quot;web-vitals&quot;: &quot;^2.1.4&quot;
  },
  &quot;scripts&quot;: {
    &quot;start&quot;: &quot;react-scripts start&quot;,
    &quot;predeploy&quot;: &quot;npm run build&quot;,
    &quot;deploy&quot;: &quot;gh-pages -d build&quot;,
    &quot;build&quot;: &quot;react-scripts build&quot;,
    &quot;test&quot;: &quot;react-scripts test&quot;,
    &quot;eject&quot;: &quot;react-scripts eject&quot;
  },
  &quot;eslintConfig&quot;: {
    &quot;extends&quot;: [
      &quot;react-app&quot;,
      &quot;react-app/jest&quot;
    ]
  },
  &quot;browserslist&quot;: {
    &quot;production&quot;: [
      &quot;&gt;0.2%&quot;,
      &quot;not dead&quot;,
      &quot;not op_mini all&quot;
    ],
    &quot;development&quot;: [
      &quot;last 1 chrome version&quot;,
      &quot;last 1 firefox version&quot;,
      &quot;last 1 safari version&quot;
    ]
  },
  &quot;devDependencies&quot;: {
    &quot;gh-pages&quot;: &quot;^5.0.0&quot;
  }
}

I have tried some fixes I found online but none of them work. If you need the repo link here it is: https://github.com/melosshabi/chat-app

I have tried changing the import from: import { HashRouter as Router, Routes, Route } from &#39;react-router-dom&#39;
to: import { HashRouter, Routes, Route } from &#39;react-router-dom&#39;

I have tried changing the app name.

I also tried changing :&lt;Route path=&quot;/chat-app/home&quot; element={&lt;Home /&gt;} /&gt;
to: &lt;Route path=&quot;/chat-app&quot; exact element={&lt;Home /&gt;} /&gt;

答案1

得分: 2

从所有路由中删除&quot;/chat-app&quot;,因为包含这部分会使绝对URL变为https://melosshabi.github.io/chat-app/#/chat-apphttps://melosshabi.github.io/chat-app/#/chat-app/signin 等。

如果需要的话,您可以在路由器上指定一个basename属性。我认为在您的情况下不会需要。

在RRDv6中,Route组件上也没有exact属性;所有路由都是精确匹配的。

function App() {
  return (
    &lt;HashRouter basename=&quot;/chat-app&quot;&gt; // &lt;-- 如果需要
      &lt;Routes&gt;
        &lt;Route path=&quot;/&quot; element={&lt;Home /&gt;} /&gt;
        &lt;Route path=&quot;/signIn&quot; element={&lt;SignIn /&gt;} /&gt;
        &lt;Route path=&quot;/signUp&quot; element={&lt;SignUp /&gt;} /&gt;
        &lt;Route path=&quot;/userProfile&quot; element={&lt;UserProfile /&gt;} /&gt;
      &lt;/Routes&gt;
    &lt;/HashRouter&gt;
  );
}
英文:

Remove &quot;/chat-app&quot; from all the routes since including this would make the absolute URLs https://melosshabi.github.io/chat-app/#/chat-app and https://melosshabi.github.io/chat-app/#/chat-app/signin, etc.

React HashRouter 在 GitHub Pages 上不渲染任何内容。

If necessary you can specify a basename prop on the router. I don't think it will be required in your case.

There is also no exact prop on the Route component; in RRDv6 all routes are always exactly matched.

function App() {
  return (
    &lt;HashRouter basename=&quot;/chat-app&quot;&gt; // &lt;-- if necessary
      &lt;Routes&gt;
        &lt;Route path=&quot;/&quot; element={&lt;Home /&gt;} /&gt;
        &lt;Route path=&quot;/signIn&quot; element={&lt;SignIn /&gt;} /&gt;
        &lt;Route path=&quot;/signUp&quot; element={&lt;SignUp /&gt;} /&gt;
        &lt;Route path=&quot;/userProfile&quot; element={&lt;UserProfile /&gt;} /&gt;
      &lt;/Routes&gt;
    &lt;/HashRouter&gt;
  );
}

huangapple
  • 本文由 发表于 2023年2月16日 04:08:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464970.html
匿名

发表评论

匿名网友

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

确定