skip to Main Content

I was setting up a react router for the first time and then i pushed it to gh-pages. It worked when i only routed the contact page. When i added more pages it did not work anymore. It does work in my localhost. My index.js looks like this:

import React from "react"
import ReactDOM from "react-dom/client"
import {App} from "./App"
import { BrowserRouter } from "react-router-dom"

const root = ReactDOM.createRoot(document.getElementById("root"))
root.render(
    <React.StrictMode>
        <BrowserRouter>
            <App/>
        </BrowserRouter>
    </React.StrictMode>
)

My App.js looks like this:

import { Route, Routes } from "react-router-dom"

import Home from "./pages/home";
import Contact from "./pages/contact";
import Nieuws from "./pages/nieuws";
import Openingstijden from "./pages/openingstijden";
import Producten from "./pages/producten";
import Prijzen from "./pages/prijzen";

export function App() {
    return (
        <Routes>
            <Route path="/KapsalonDeTussenstop/" element={<Home />} />
            <Route path="/KapsalonDeTussenstop/contact" element={<Contact />} />
            <Route path="/KapsalonDeTussenstop/nieuws" element={<Nieuws />} />
            <Route path="/KapsalonDeTussenstop/openingstijden" element={<Openingstijden />} />
            <Route path="/KapsalonDeTussenstop/prijzen" element={<Prijzen />} />
            <Route path="/KapsalonDeTussenstop/producten" element={<Producten />} />
        </Routes>
    )
}

My Home page does work (see image) so i think my gh-pages is good.
What do i need to do to fix this?

I tried to follow instructions and I tried to do the routing it worked, but after that not anymore. I also tried hashrouter but then it is also not working on localhost.

2

Answers


  1. Chosen as BEST ANSWER

    I kind of solved the problem. It did not work if i types in the path like /contact. The button with the exact path was also not working. This button did work: <Link to="/contact">Go to About Page</Link>


  2. First of all, use exact in <Route />

            <Routes>
                <Route exact path="/KapsalonDeTussenstop/" element={<Home />} />
                <Route exact path="/KapsalonDeTussenstop/contact" element={<Contact />} />
                <Route exact path="/KapsalonDeTussenstop/nieuws" element={<Nieuws />} />
                <Route exact path="/KapsalonDeTussenstop/openingstijden" element={<Openingstijden />} />
                <Route exact path="/KapsalonDeTussenstop/prijzen" element={<Prijzen />} />
                <Route exact path="/KapsalonDeTussenstop/producten" element={<Producten />} />
            </Routes>
    

    Also add a Homepage Route : / by writing.

    <Route exact path="/" element={ <Home /> } />

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search