React Router not working with Github Pages

  1. If deploying to GitHub, ensure there is a "homepage" entry for where you are hosting it in Github.

    Example:

     "homepage": "https://github.com/amodhakal/portfolio",
    
  2. Switch to the HashRouter since GitHub pages doesn’t support the tech used by the BrowserRouter.

    index

     import React from 'react';
     import ReactDOM from 'react-dom/client';
     import { HashRouter } from 'react-router-dom'
     import App from './App';
     import './styles/index.css';
    
     ReactDOM.createRoot(document.getElementById('root')).render(
       <React.StrictMode>
         <HashRouter>
           <App />
         </HashRouter>
       </React.StrictMode>
     );
    

For more details see the create-react-app docs for deploying to GitHub Pages and notes on client-side routing.

Leave a Comment