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

131
Visualizações
React router always display at the bottom the Error page

I'm having a problem regarding using of error page in my react app. The 404 page always shows at the bottom of every page that I render. I'm new to react. I hope someone can help me.

This is my App.js

import {BrowserRouter as Router,Switch,Route} from 'react-router-dom';

import Login from './components/auth/Login';
import Register from './components/auth/Register';
import ErrorPage from './components/ErrorPage';
import Order from './components/Order';
import Navbar from './components/partials/Navbar';
import Footer from './components/partials/Footer';
import Shop from './components/Shop';
import ItemDetails from './components/ItemDetails';
import Cart from './components/Cart';
import Customize from './components/Customize';


const App = () => {

  return (
    <>
     <Router>
       <Switch>
        
         <Route exact path='/login'>
           <Login />
         </Route>
         
         <Route exact path='/register'>
           <Register />
         </Route>

         <div>
            <Navbar />

              <Route exact path='/'>
                <Shop />
              </Route>

              <Route exact path='/order'>
                <Order />
              </Route>

              <Route exact path='/item/details'>
                <ItemDetails />
              </Route>

              <Route exact path='/cart'> 
                <Cart />
              </Route>

              <Route exact path='/customize'>
                <Customize />
              </Route>

              <Route component={ErrorPage} />
          </div>   
          
       </Switch>
     </Router> 
     <Footer />
    </>
  );
}

export default App;

I searched about handling error page in react and I see that the order of routes is important but I don't get why I'm still getting the error page even it's in the bottom. Thank you guys.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

That's because the switch component returns the first child at the root that meets the path condition. If the path condition doesn't exist it's evaluated as true. In your case you have 3 child Login, Register and the div which will always be evaluated to true. So just move all routes to the root of your switch:

<Router>
    <Switch>

        <Route exact path='/login'>
            <Login />
        </Route>
        
        <Route exact path='/register'>
            <Register />
        </Route>

        <div>
            <Navbar />
            <Switch>
                <Route exact path='/'>
                    <Shop />
                </Route>

                <Route exact path='/order'>
                    <Order />
                </Route>

                <Route exact path='/item/details'>
                    <ItemDetails />
                </Route>

                <Route exact path='/cart'> 
                    <Cart />
                </Route>

                <Route exact path='/customize'>
                    <Customize />
                </Route>

                <Route component={ErrorPage} />
            </Switch>
        </div>   
        
    </Switch>
</Router> 

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