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

187
Views
Javascript - if statements regarding returns and views

I want to consolidate these if statements to make this efficient as possible while still having it be able to perform case 1 alone, case 2 alone, case 1 and 2 together, and neither case 1 nor case 2. Is there a way to do this?

if(x >= 100 && y < 100){
return(
    <View>
        <Text>
            this is case 1
        </Text>
    </View>          
)}            
if(x < 100 && y >= 100){
return(
    <View>
        <Text>
            this is case 2
        </Text>
    </View>          
)}
if(x < 100 && y < 100){
return(
    <View>
        <Text>
            this is case 1
        </Text>
    </View>
    <View>
        <Text>
            this is case 2
        </Text>
    </View>                    
)}
if(x >= && y >= 100){
return(
    <View>
        <Text>
            this is case 4
        </Text>
    </View>          
)}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Put your return value in a variable that you can concatenate for each case.

retval = '';
if (y < 100) {
    retval += 
        <View>
            <Text>
                this is case 1
            </Text>
        </View>          
    ;
}
if (x < 100) {
    retval += 
        <View>
            <Text>
                this is case 2
            </Text>
        </View>
    ;
}
if (retval == '') {
    // neither case
    return(
        <View>
            <Text>
                this is case 4
            </Text>
        </View>          
    );
} else {
    return retval;
}
about 4 years ago · Juan Pablo Isaza Report

0

You could collect the wanted strings and return either the formatted collection or case four.

const take = [];

if (x < 100) take.push('this is case 2');
if (y < 100) take.push('this is case 1');

return take.length
    ? take.map(/* your formatting */)
    : 'this is case 4';
about 4 years ago · Juan Pablo Isaza Report

0

Try this:

const el1 = (
    <View>
        <Text>
            this is case 1
        </Text>
    </View>          
)
const el2 = (
    <View>
        <Text>
            this is case 2
        </Text>
    </View>    
)
...

const conditions = {
    el1,
    el2,
    ...
}

if (x >= 100 && y < 100)
    return conditions.el1
else if (x < 100 && y >= 100)
    return conditions.el2
else if (x < 100 && y < 100)
    return conditions.el1 + conditions.el2
else
    return conditions.default || ''
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!