Jasur Kurbanov
Jasur Kurbanov

Reputation: 830

Loop and render elements not working in React-native

I couldn't understand why it is not working. It is showing me error cannot find name 'contentItem'. Any suggestions ?

const CONTENT = [
{
    title: 'Лицевой счет 256215492543',
    sub_title: 'ул.Неизвестная, 78/9,',
    count: true
},
{
    title: 'Лицевой счет 12356215492543',
    sub_title: 'ул.Неизвестная, 72/7,',
    count: false
},
....
renderContent(section, _, isActive, CONTENT) {
    return (   
      CONTENT.map(contentItem => (
        if (contentItem.special) {
          return(
            <View>
              <Text>{contentItem.title} spec</Text>
          </View>
          )
        }
        return(
          <View style={{backgroundColor:'red'}}>
            <Text>{contentItem.sub_title} </Text>
          </View>
        )
      });

Basic idea, is to show TITLE if "count" is TRUE, if "count" false show SUB_TITLE

Upvotes: 2

Views: 537

Answers (1)

Lenoarod
Lenoarod

Reputation: 3610

I change the map function, in the js array map function, it will have three arguments, you can look at the API about map

const CONTENT = [
{
    title: 'Лицевой счет 256215492543',
    sub_title: 'ул.Неизвестная, 78/9,',
    count: true
},
{
    title: 'Лицевой счет 12356215492543',
    sub_title: 'ул.Неизвестная, 72/7,',
    count: false
},
renderContent(section, _, isActive, CONTENT) {
    return (   
      CONTENT.map((item,index)=> {
        if (item.count) {
          return(
            <View key={index}>
              <Text>{item.title} spec</Text>
          </View>
          )
        }
        return(
          <View key={index} style={{backgroundColor:'red'}}>
            <Text>{item.sub_title} </Text>
          </View>
        )
      }};

Upvotes: 3

Related Questions