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

190
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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