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);
...
CodePudding user response:
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.
