首页 > 解决方案 > 更新 Flatlist 中的单个项目

问题描述

嘿伙计们,我需要更新 FlatList 数据中的单个项目。我想计算购物车中的物品。当我单击 + 或 - 时,它没有任何变化。但是当我使用平面列表的额外数据道具时,它的所有计数都发生了变化..

    class Cart extends Component {
    state:
      {
         count:0
      }


   render() {
    const cartcount = <View style= {{height:40,
                                     flexDirection:'row',
                                     justifyContent:'space-around'}}>
      <TouchableOpacity onPress={()=>
                           {this.setState({
        count: this.state.count-1
      })}}>
        <Text>-</Text></TouchableOpacity><Text
        > { this.state.count >=0 && this.state.count !== 0 ? this.state.count: 1 }</Text>
        <TouchableOpacity onPress={()=>
                            {this.setState({ count: this.state.count+1})}}><Text>+</Text></TouchableOpacity>
    </View>;

return (

<FlatList data={this.state.cart}
        renderItem={({item,index}) =>
                      <View>{ cartcount }</View>
    />

标签: react-nativereact-native-androidreact-native-flatlist

解决方案


推荐阅读