admin管理员组文章数量:1302384
I have been trying to develop a list which has cards and list item from data source. I have successfully brought up the list, but what i want to achieve is that when ever user touches a Item in the list, the item's color should change.
Most importantly only one item should be selected. How to achieve this? I have grabbed the value of the data by using redux actions and reducers. But, i don't have any idea how to achieve this selection process.
My flatList code:
<FlatList
horizontal={true}
data={this.qtyList}
keyExtractor={item => item.id.toString()}
showsHorizontalScrollIndicator={false}
renderItem={({ item }) => (
<TouchableHighlight
onPress={() => {
}}
>
<Card
containerStyle={{ borderRadius: 5 }}
>
<Text>
{item.qty}
</Text>
</Card>
</TouchableHighlight>
)}
/>
Please provide step by step instructions, as i am totally a beginner. i don't want to do this with help of redux, so ponent level state would be great help.
I have been trying to develop a list which has cards and list item from data source. I have successfully brought up the list, but what i want to achieve is that when ever user touches a Item in the list, the item's color should change.
Most importantly only one item should be selected. How to achieve this? I have grabbed the value of the data by using redux actions and reducers. But, i don't have any idea how to achieve this selection process.
My flatList code:
<FlatList
horizontal={true}
data={this.qtyList}
keyExtractor={item => item.id.toString()}
showsHorizontalScrollIndicator={false}
renderItem={({ item }) => (
<TouchableHighlight
onPress={() => {
}}
>
<Card
containerStyle={{ borderRadius: 5 }}
>
<Text>
{item.qty}
</Text>
</Card>
</TouchableHighlight>
)}
/>
Please provide step by step instructions, as i am totally a beginner. i don't want to do this with help of redux, so ponent level state would be great help.
Share edited Jan 8, 2019 at 19:13 James Z 12.3k10 gold badges27 silver badges47 bronze badges asked Jan 8, 2019 at 15:02 ArvindhArvindh 6202 gold badges12 silver badges27 bronze badges2 Answers
Reset to default 8You need a function which sets state when the user click an item of FlatList. When the state changes, ponent style will change for showing selected item. And you should set extraData to FlatList for rendering when the state changes.
class Second extends React.Component {
constructor(props) {
super(props);
this.state = {
selectedItem: null
};
}
onPressHandler(id) {
this.setState({selectedItem: id});
}
render() {
return (
<View>
<FlatList
extraData={this.state.selectedItem} //Must implemented
horizontal={true}
data={qtyList}
keyExtractor={item => item.id.toString()}
showsHorizontalScrollIndicator={false}
renderItem={({item}) => (
<TouchableOpacity
onPress={() => this.onPressHandler(item.id)}>
<Card
containerStyle={this.state.selectedItem === item.id ? {
borderRadius: 5,
backgroundColor: "#000000"
} : {borderRadius: 5, backgroundColor: "#a1a1a1"}}>
<Text>{item.qty}</Text>
</Card>
</TouchableOpacity>
)}
/>
</View>
);
}
}
You should store the id of the selected item in the state:
<TouchableHighlight
onPress={() => {
this.setState({ itemSelected: item.id }) <== your item must have a unique id
}}
>
Then, for example in your card ponent you can do:
<Card
containerStyle={{
borderRadius: 5,
backgroungColor: this.state.itemSelected === item.id ? 'red', 'white',
}}
>
In addition, you have to add extraData={this.state}
to your flatlist. Here's the link to the doc
版权声明:本文标题:javascript - How to change color of an component when its pressed in React Native FlatList - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741666347a2391333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论