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

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

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 Denunciar

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