1

I did an "IF" to do dynamic text, if is null do show something,else shows another.But it is returning blank.What I did wrong ?

<View style={styles.rightContainer}>
    { () =>{        
        if(this.state.sicafSource.IdStatusManutencao == null){

            return(
                <View style={[{paddingHorizontal:10, marginTop:5,borderRadius:5},this.ReturnColor("Não achei na api","ATIVO")]}>
                        <Text style={[{ textAlign: 'right' }, styles.swipeCardRightTextS]}>
                                Sem informação 
                        </Text>
                </View>
            );

    }
    else{

        return(
            <View style={[{paddingHorizontal:10, marginTop:5,borderRadius:5},this.ReturnColor("Não achei na api","ATIVO")]}>
                <Text style={[{ textAlign: 'right' }, styles.swipeCardRightTextS]}>
                    api resposta true 
                </Text>
            </View>          

        );
    }
}
    }
</View>
Cory Danielson
  • 14,314
  • 3
  • 44
  • 51
  • 1
    Start by cleaning up your indentation to make your code more clear. Also, why is the `if` statement inside an anonymous function? – Code-Apprentice Jun 05 '19 at 17:42

2 Answers2

3

You need to use the ternary operator in JSX. Everything within {} needs to be an expression (something that returns a value). If's are a statement.

{this.state.sicafSource.IdStatusManutencao == null ? (
    <div>Return this when true</div>
) : (
    <div>Return this when false</div>
)
Cory Danielson
  • 14,314
  • 3
  • 44
  • 51
0

One solution is to call your anonymous function so that the if statement can be evaluated:

() => {
  // ...
} () // add these

However using a ternary operator or boolean operators is cleaner. Instead of

if (A) {
  B;
} else {
  C;
}

You can do either

A ? B : C

or

A && B || C
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268