0

so i'm trying to get the key of a ListItem, generated in a loop for, but i get a undefined value.

<ListItem id={i} key = {i} button onPress={() => alert(this.key)}>

If someone can help me with that ?

Patrick Trentin
  • 7,126
  • 3
  • 23
  • 40

2 Answers2

1

You can't access key. It is a special string attribute. Rather use the i variable because in your case it is the key.

<ListItem id={i} key = {i} button onPress={() => alert(i)}>
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
0

It's not working, look the code :

for(var i = 0; i< data.service.length; i++ )
    {
    listArr.push(
        <ListItem key={i} button onPress={(key) => this.ChangeHome(key) }>
          <Thumbnail square size={80} source={{ uri: data.service[i].logo }} />
          <Body>
          <Text>{data.service[i].id}</Text>
            <Text>{data.service[i].title}</Text>
            <Text note>{data.service[i].info}</Text>
          </Body>
        </ListItem>

    );
}

As you can see if I put only the i, the alert will show 2 ( the end of the loop for) instead the id of the list item ( 0 or 1 ) :/