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

95
Views
React Router Dom v6 render not redirecting
          <Route
            exact
            path="/register"
            element={<Register />}
            render={() => !isAuthenticated ? <Register/> : <Navigate replace to="/login" />}
          /> 

Okay so i want to redirect to login if user is not authenticated and "isAuthenticated" is normally set to false but it wont redirect me. Not a single error is logged. I replaced <Navigate /> with console.log("something") to see if there is something wrong with component imported from React-Router-Dom but that "something" is not logging. I tried all still don't know what is wrong. Thanks

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

  1. Logic error?

isAuthenticated = false by default. You want:

  • false > 'Login'
  • true > <Register/>

But you put !isAuthenticated so it's the inverse:

  • false > <Register/>
  • true > 'Login'
  1. Change the ternary into something more easily comprehensive.

Why not: isAuthenticated ? <Navigate replace to="/login" /> : <Register/>.
Save me the headache.

  1. Navigate properties

Be explicit with the properties

<Navigate replace to="/login" />
/*or replace={false} */ <Navigate replace={true} to="/login" />
about 4 years ago · Juan Pablo Isaza Report

0

You are mixing react-router-dom v5 and v6 syntaxes. The RRDv6 Route components no longer take a render function prop (nor a component prop or children function prop) and now take only a single element prop taking JSX to render the routed content.

Remove the render prop and move the conditional logic into the element prop.

<Route
  path="/register"
  element={!isAuthenticated ? <Register /> : <Navigate replace to="/login" />}
/>

If this is a pattern you use often then abstract it into an AuthLayout component that conditionally renders an Outlet for nested routes you want to protect or the Navigate component to redirect.

const AuthLayout = ({ isAuthenticated }) => 
  !isAuthenticated
    ? <Outlet />
    : <Navigate replace to="/login" />;

...

<Route element={<AuthLayout isAuthenticated={isAuthenticated} />}>
  <Route path="/register" element={<Register />} />
</Route>

Note: I think you've possibly mixed up what you want to render when isAuthenticated is true as generally you'd want to redirect to the authentication route when not authenticated.

In other words, if authenticated, render the routed component in the outlet, otherwise redirect to login route.

const AuthLayout = ({ isAuthenticated }) => 
  isAuthenticated
    ? <Outlet />
    : <Navigate replace to="/login" />;
about 4 years ago · Juan Pablo Isaza Report

0

You should lose "exact" from the routes, as it was changed in v6 version.

https://reactrouter.com/docs/en/v6/upgrading/v5

as per react router

<Route exact> is gone. Instead, routes with descendant routes (defined in other components) use a trailing * in their path to indicate they match deeply

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!