70

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?

6 Answers 6

107
<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>
Sign up to request clarification or add additional context in comments.

7 Comments

Making the asterisk '*' route as the first route, you are hiding all other routes, hence all requests will go through the NotFound page. you must make it the last route.
@AhmedHany. no, This will happen if you are using the old versions. now " > v6 " they changed the way it works. it's a lot better now.
@Haitham6373 so what's better to have it on top instead of bottom? what's the diff in v6?
@Haitham6373 It doesn't work if I navigate to <baseUrl>/someRandomText/. Note the trailing slash.
Is this supposed to work with nested routes also?
|
31

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

1 Comment

how do you handle nested route?
12

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>

Comments

12

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

Comments

8

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)

2 The easiest example is here

Comments

0

Create component and inside component create function NotFound.

in app.js create and give path="*" and element should be NotFound function.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.