Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

274
Vistas
Redirect to 404 not found page while any of the paths get unmatched?
 <Routes>
  {!userlog ? (
    <Route path="/" element={<AuthLayout />}>
      <Route index element={<Login />} />
      <Route path="/login" element={<Login />} />
      <Route path="/signin" element={<Login />} />
      <Route path="/forgotpassword" element={<ForgotPassword />} />
      <Route path="/changepassword" element={<ChangePassword />} />
    </Route>
  ) : userlog?.user_type===1?(
  <Route path="/" element={<HomeLayout />}>
    <Route index element={<HomePage />} />
    <Route path="viewproduct/:id" element={<ViewPage />} />
    <Route path="cartlist" element={<CartList />} />
  </Route>
  ):
  <Route path="/" element={<AdminLayout />}>
    <Route index element={<Dashboard />} />
    <Route path="listproducts" element={<ProductsList />} />
  </Route> 
  } 
  <Route path="*" element={<Navigate to={"/"} />} />
</Routes>

What i want,
Scenario 1(user not logged in): if user try to navigate "/cartlist" it's redirect to the login page. If user try navigate something not an part of route path's want to navigate 404 not found page.

Scenario 2(user logged in): if user try to navigate "/login" it's redirect to the dashboard page. If user try navigate something not an part of route path's want to navigate 404 not found page.

Now what i get is,
User try to navigate any other routes it's navigate to "/" page.

NOTE: I don't want to put private route for every Route's and Also i want routing path like this only..

Thanks in advance....

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Ofcourse due to this line, it will go to "/":

<Route path="*" element={<Navigate to={"/"} />} />

With the new react-router v6 you can use this by changing:

// 404 route
<Route path="*" element={<NotFoundPage />} />

NotFoundPage will be your component to tell users that page is not found.

Also, for these, i think you need to put a forward slash / before pathname

<Route path="/viewproduct/:id" element={<ViewPage />} />
<Route path="/cartlist" element={<CartList />} />
<Route path="/listproducts" element={<ProductsList />} />
about 4 years ago · Juan Pablo Isaza Denunciar

0

The reason it always resolves to "/" is because it will choose the first route which matches the given pattern, even if it is a partial match. Since all of your routes begin with "/" eg. "/login" etc it always matches "/".

You can prevent this with the "exact" parameter:

<Route exact path="/" element={<AuthLayout />}>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda