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

110
Views
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 answers
Answer question

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