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

130
Vistas
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 Respuestas
Responde la pregunta

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 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