Search code examples
react-nativeif-statementconditional-operator

Is there a way to use 3 arguments on a inline if statement?


Today I have this inline if which works fine for me

                  <View style={{justifyContent: 'center'}}>
                    {returnDayPercentageChange() > 0 ? (
                      <Icon
                        style={{color: '#1CDC93', marginTop: 3}}
                        name="caretup"
                        size={10}
                      />
                    ) : (
                      <Icon
                        style={{color: '#FF6767', marginTop: 3}}
                        name="caretdown"
                        size={10}
                      />
                    )}
                  </View>

It works well but I want besides that to have no Icon when the returnDayPercentage === 0, how can I do that?


Solution

  • You can nest ternary operators.

    {returnDayPercentageChange() > 0 ? (
         <Icon
           style={{color: '#1CDC93', marginTop: 3}}
           name="caretup"
           size={10}
         />) : (
        returnDayPercentageChange() < 0 ? 
         <Icon
            style={{color: '#FF6767', marginTop: 3}}
            name="caretdown"
            size={10}
         /> : null)}