Home > Net >  How to apply conditionals in a nested map function?
How to apply conditionals in a nested map function?

Time:12-02

I want to handle each string of my item.fieldtype result, but i'm not finding a way to do it someone knows how?

....
      <SafeAreaView style={{flex: 1}}>
          <View>
            {Object.keys(Body).length > 0 ? (
              Body.map(item => <Text key={uuid.v4()}>{item.field}</Text>
              // if (item.fieldtype == "Numeric") <Text>Numeric</text> ===> apply here
              //else if (item.fieldtype == "Text" ) <Text>Text</text> ===> "" ""      
              )
            ) : (
              <Text>Testado</Text>
            )}
          </View>
        </SafeAreaView>
...

CodePudding user response:

You can use a fragment tag (or just a View) and return multiple items in the map:

<SafeAreaView style={{flex: 1}}>
  <View>
    {Object.keys(Body).length > 0 ? 
      (Body.map(item => 
       <React.Fragment key={uuid.v4()}>
          <Text>{item.field}</Text>
          {item.fieldtype == "Numeric" ? <Text>Numeric</Text> : null}
          {item.fieldtype == "Text" ? <Text>Text</Text> : null}
       </React.Fragment>))
      : (<Text>Testado</Text>)}
  </View>
</SafeAreaView>

(Note that it would be ideal if you had useable stable ids rather than generating a new uuid for each item on each render)

  • Related