英文:
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 './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"
}
}
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 'react-router-dom'
to: import { HashRouter, Routes, Route } from 'react-router-dom'
I have tried changing the app name.
I also tried changing :<Route path="/chat-app/home" element={<Home />} />
to: <Route path="/chat-app" exact element={<Home />} />
答案1
得分: 2
从所有路由中删除"/chat-app"
,因为包含这部分会使绝对URL变为https://melosshabi.github.io/chat-app/#/chat-app 和 https://melosshabi.github.io/chat-app/#/chat-app/signin 等。
如果需要的话,您可以在路由器上指定一个basename
属性。我认为在您的情况下不会需要。
在RRDv6中,Route
组件上也没有exact
属性;所有路由都是精确匹配的。
function App() {
return (
<HashRouter basename="/chat-app"> // <-- 如果需要
<Routes>
<Route path="/" element={<Home />} />
<Route path="/signIn" element={<SignIn />} />
<Route path="/signUp" element={<SignUp />} />
<Route path="/userProfile" element={<UserProfile />} />
</Routes>
</HashRouter>
);
}
英文:
Remove "/chat-app"
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.
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 (
<HashRouter basename="/chat-app"> // <-- if necessary
<Routes>
<Route path="/" element={<Home />} />
<Route path="/signIn" element={<SignIn />} />
<Route path="/signUp" element={<SignUp />} />
<Route path="/userProfile" element={<UserProfile />} />
</Routes>
</HashRouter>
);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论