Reputation: 1175
I just upgraded to v6beta for react-router-dom because I wanted to organize my routes, but the 404 page is now broken:
export function AllRoutes(props) {
return(
<Routes>
<Route path="/about-us">
<Route path="contact"> <AboutContact/> </Route>
<Route path="our-logo"> <AboutLogo/> </Route>
<Route path="the-mission"> <AboutMission/> </Route>
<Route path="the-team"> <AboutTeam/> </Route>
<Route path="our-work"> <AboutWork/> </Route>
</Route>
<Route path="/user">
<Route path="sign-in"> <UserSignIn/> </Route>
<Route path="sign-up"> <UserSignUp/> </Route>
</Route>
<Route path="/">
<Home/>
</Route>
<Route >
<NotFound/>
</Route>
</Routes>
)
}
So everything works as expected (including the home page) except the not found page, which does not work even when adding path="/*"
or path="*"
Any easy solution for this?
Upvotes: 69
Views: 74614
Reputation: 83
Create component and inside component create function NotFound.
in app.js create and give path="*" and element should be NotFound function.
Upvotes: 0
Reputation: 554
You need to render a Route with a path of *
, and React Router will make sure to only render it if none of the other Routes match.
If your 404 component name is PageNotFound
:
<Route path="*" element={<PageNotFound/>} />
1 Stackblitz link (its implementation is a little bit different)
Upvotes: 8
Reputation: 121
You can use
<Route path="*" element={ <Navigate to="/404" replace />} />
this way you have a redirection and a url replacement
for example :
import React from 'react'
import { BrowserRouter as Router, Routes , Route, Navigate } from 'react-router-dom'
import Home from '../../pages/Home'
import Account from '../../pages/Account'
import Contact from '../../pages/Contact'
import NotFound from '../../pages/NotFound'
const AllRoutes= () => {
return(
<Router>
<Routes>
<Route path="/" element={ <Home /> } />
<Route path="/Account" element={ <Account /> } />
<Route path="/Contact" element={ <Contact /> } />
<Route path="/404" element={ <NotFound /> } />
<Route path="*" element={ <Navigate to="/404" replace />} />
</Routes>
</Router>
)
}
export default AllRoutes
Upvotes: 12
Reputation: 181
react router v6 don't have Switch so you need to declare path='*'
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact/>} />
<Route path="*" element={<NotFound/>} />
</Routes>
</BrowserRouter>
Upvotes: 12
Reputation: 361
React Router v6 does not use exact attribute to match the exact routes. By default it matches the exact route. To match any value use "*" in the route path.
<BrowserRouter>
<div className="App">
<Header />
</div>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/profile" element={<Profile />} />
<Route path="*" element={<NotFound />} />
</Routes>
</BrowserRouter> enter code here
Upvotes: 31
Reputation: 1299
<Routes>
// Use it in this way, and it should work:
<Route path='*' element={<NotFound />} />
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/settings" element={<Settings />} />
</Routes>
Upvotes: 107