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

109
Vistas
How to return a component only if the component returns something in a single line?

I have this code and I would like to be able to write this in a single line of code. Would that be possible?

If the component returns elements, return these elements from the parent component. Otherwise, do not return at all, but continue the script.

...
const c = someComponent(var);
if (c) return c;
...

Edit

The purpose is that I want to make the code look cleaner, so ideally I would want it to look like this, where this function potentially replaces the output of the parent component and stops further execution.

...
checkSomeVariables(var1, var2);
...
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

By "single line" I'm going to assume you mean a single statement or expression (since you could write the code you have on a single line if you wanted).

You can't if you don't already have an identifier to assign it to (c). You don't appear to in your example.

If you did, you could do this (though I don't recommend it):

if ((c = someComponent(var))) return c;

but again, c would have to already exist (and be writable).


If the issue you have with the code in the question is that you have c lying around when you're done (and can't use it again if you want to do this again), you could scope it to an anonymous block:

{
    const c = someComponent(var);
    if (c) returnc;
}

Then c doesn't exist outside that block.

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