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

236
Views
React Javascript comparison in return statement

I'm creating a webapp in react js and following this tutorials: https://www.youtube.com/watch?v=PKwu15ldZ7k&t=2002s can someone explain this piece of code to me.

return currentUser ? <Outlet /> : <Navigate to="/login" />;

I understand that if currentUser is null then it redirects to login. Is there a way to make it return a different page if not null?

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

0

This is basically a one-liner if statement. It reads:

if (currentUser) { // If currentUser is different than undefined/null/false
  return <Outlet />
} else {
  return <Navigate to="/login" />
}

As this point, you can choose to return anything you want in either case. You could redirect to any page you have, you could return any other components, etc.

about 4 years ago · Juan Pablo Isaza Report

0

This piece of code return currentUser ? <Outlet /> : <Navigate to="/login" />; is called a ternary operator. In other words, if currentUser is true then return the Outlet component otherwise return the Navigate component. It is a shortened way to write an if/else statement.

Here is more on ternary operators: MDN Web Docs: ternary operator

If you want it to redirect to a different url when it returns the Navigate component then you simply changed the /login to whatever url you want to load on null /someOtherURL

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!