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

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

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 Relatório

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 Relatório

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