I've previously created and deployed single-page react apps on GitHub and netlify and they all worked fine. the problem is with multi-page apps using react-router. to test this I tried several times with different apps and as soon as I implement react-router and link to different pages, they become blank after deployment. here's a test app:
index.js
import React from "react";
import ReactDOM from "react-dom/client";
import { BrowserRouter } from "react-router-dom";
import "./index.css";
import App from "./App";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>
);
App.js
import React from "react";
import { Routes, Route } from "react-router-dom";
import Index from "./pages/Index";
import About from "./pages/About";
import Products from "./pages/Products";
import Navbar from "./components/Navbar";
const App = () => {
return (
<>
<Navbar />
<Routes>
<Route exact path="/" element={<Index />} />
<Route path="/about" element={<About />} />
<Route path="/products" element={<Products />} />
</Routes>
</>
);
};
export default App;
Navbar.js component
import React from "react";
const Navbar = () => {
return (
<>
<nav className="navbar">
<ul>
<li>
<a href="/">home</a>
</li>
<li>
<a href="/about">about</a>
</li>
<li>
<a href="/products">products</a>
</li>
</ul>
</nav>
</>
);
};
export default Navbar;
Package.json
{
"name": "test-app",
"version": "0.1.0",
"homepage": "https://EricSsSnake/github.io/test-app",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.4.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"start": "react-scripts start",
"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": "^4.0.0"
}
}
plus 3 pages to navigate to.
When I deploy this on GitHub all I get is a blank page with these errors. screenshot
for deployment, I follow this tutorial: react-gh-pages
I'd really appreciate your help guys. this issue has stopped me from making any progress. I spent 3 weeks making my portfolio and now it's just sitting there.
CodePudding user response:
change :
"homepage": "https://EricSsSnake/github.io/test-app",
to :
"homepage": "https://EricSsSnake.github.io/test-app",
and use HashRouter instead of BrowserRouter . in your case change your index.js like this :
import React from "react";
import ReactDOM from "react-dom/client";
import { HashRouter } from "react-router-dom";
import "./index.css";
import App from "./App";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<HashRouter>
<App />
</HashRouter>
</React.StrictMode>
);
Note : githup pages do not support React Router with browserHistory
because it's using HTML5 history API that's why you can switch to to routing with hashes ( HashRouter
in react router) check in the official doc of create-react-app
CodePudding user response:
Is there any error in the console?
If not, try using <Link />
component imported from 'react-router-dom' or 'react-router' instead of '' provided by html, because <a>
will trigger a page refresh and <Link />
won't.
hope this answer help you