Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda