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

265
Vistas
react-router-dom v6 renders a Route when it is supposed to not render it

I want to render private routes for logged in users. For some reason react-router-dom (v6) renders private routes even if PrivateRoute returns null. Also, I couldn't see any console.logs from inside PrivateRoute. Any ideas?

EDIT

It renders Dashboard when you go to localhost/dashboard and Settings when you go to localhost/settings, even though both components are passed to PrivateRoute, which returns null.

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import {BrowserRouter} from 'react-router-dom';

import App from './App';

ReactDOM.render(
    <BrowserRouter>
        <App />
    </BrowserRouter>
    ,  document.getElementById('root'));

App.js

import {Route, Routes} from 'react-router-dom';

import {Home} from './components/Home';
import {Pricing} from './components/Pricing';
import {Dashboard} from './components/Dashboard';
import {Settings} from './components/Settings';
import {Login} from './components/Login';
import {Header} from './components/Header';
import {PrivateRoute} from './components/PrivateRoute';

const App = () => {
  

  return (
    <div>
      <Header />

      <Routes>
        <Route path='/' element={<Home/>} />
        <Route path='/pricing' element={<Pricing />} />
        <PrivateRoute path='/dashboard' element={<Dashboard />} />
        <PrivateRoute path='/settings' element={<Settings />} />
        <Route path='/login' element={<Login />} />
      </Routes>
    </div>
  );
};

export default App;

PrivateRoute.js

export const PrivateRoute = ({path, element}) => {
    console.log('PrivateRoute');    **// couldn't see this in the console**

    return null;
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Unfortunately, according to the documentation, the new way to do this looks like this:

<Route
    path="/protected"
    element={
        <RequireAuth>
            <ProtectedPage />
        </RequireAuth>
    }
/>

https://reactrouter.com/docs/en/v6/examples/auth

Full code exemple: https://stackblitz.com/github/remix-run/react-router/tree/main/examples/auth?file=src/App.tsx

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