Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!