Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

268
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda