gpt4 book ai didi

javascript - react native : Change and keep the background color of only onPressed item inside Flatlist

转载 作者:行者123 更新时间:2023-11-30 19:52:23 26 4
gpt4 key购买 nike

我已经通过将 item.id 分配给状态来设法更改 Flatlist 中 onPressed 按钮的背景颜色。我的问题是,当我按下另一个按钮时,之前按下的按钮变回初始背景颜色。如何在按下另一个按钮时保持新的背景颜色?

 onPressButtonarrived(item.id) {
this.setState({ disablearrived: item.id })
this.setState({ colorarrived: item.id })
}


<View style={{backgroundColor: item.id === this.state.colorarrived ? '#D6D6D6' : '#E5C454'}}>
<TouchableOpacity
onPress={() => {this.onPressButtonarrived(item.id)}} disabled={item.id === this.state.disablearrived ? true : false}>
<Text>Arrival</Text>
</TouchableOpacity>

最佳答案

您需要为每个项目设置一些状态值:

 onPressButtonarrived(item.id) {
this.setState({ [item.id +'-disablearrived']: !this.state[item.id] });
this.setState({ [item.id +'-colorarrived']: !this.state[item.id] });
// that should toggle some true false vars for each item.id
}


<View style={{backgroundColor: this.state[item.id +'-colorarrived'] ? '#D6D6D6' : '#E5C454'}}>
<TouchableOpacity
onPress={() => {this.onPressButtonarrived(item.id)}} disabled={!!this.state[item.id +'-disablearrived']}>
<Text>Arrival</Text>
</TouchableOpacity>

关于javascript - react native : Change and keep the background color of only onPressed item inside Flatlist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54349394/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com