Selecting a Native-Base Listitem

Asked

Viewed 173 times

1

I currently use Native-base for my application

I have a Listitem that lists dummy entries and I would like to keep the item pressed so that the id is saved as true in an array. Someone who can shed some light on how to proceed ?

state = {
    selected: [] ,
};

onPressAction = (key = int) => {
    if (this.state.selected.indexOf(key) < 0) {
        tmpSelected = this.state.selected
        tmpSelected.push(key)
        tmpSelected[key] = true;

        this.setState({
            selected: tmpSelected,
        })
        console.log('add '+key);
    }
    else {
        var tmpSelected = this.state.selected.slice()
        tmpSelected.splice(key, 1);

        this.setState({
            selected: tmpSelected
        })
        console.log('remove '+key);
    }
    console.log(tmpSelected);
};

<ListItem
     key={data.id}
     onPress={() => this.onPressAction(data.id)}
     selected={this.state.selected[data.id] ? false : true}
     avatar
>
    <Left>
        <Thumbnail source={{ uri: 'https://en.facebookbrand.com/wp-content/uploads/2016/05/FB-fLogo-Blue-broadcast-2.png' }} />
    </Left>
    <Body>
        <Text>{data.sitename}</Text>
        <Text note>{data.url}</Text>
    </Body>
    <Right>
        <Text note>View</Text>
    </Right>
</ListItem>
  • Have you tried passing the data.id as a parameter for the onPressAction function? Then you put in a console.log and check that the id is being passed correctly.

  • I managed to a certain point to pass, after a lot of struggle I understood that I had to replace onPress={this.onPressAction(data.id)} for onPress={() => this.onPressAction(data.id)} but now I am in trouble because it is not correctly stored the array. Where can I be wrong ? (I added the function in the topic)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.