Reputation: 2959
I am trying to make a list of items where I get the value and then store it in redux to dispatch it later in DB. But everything works well and I just got stuck in getting the value of the selected item.
So here is my category list view:
<View style={{ flex: 1 }}>
<ScrollView style={{ backgroundColor: '#ffffff', }}>
<View style={{ flex: 1, flexDirection: 'column', marginTop: 65, margin: 40 }}>
<Text style={{ fontSize: 40 }}>Job Category</Text>
<FlatList
style={{ marginTop: 20 }}
data={this.state.jobCategory}
renderItem={({ item }) => <ListItem data={item} value={item.value} />}
/>
</View>
</ScrollView>
<TouchableHighlight onPress={handleSubmit(_categorySelected)} style={{ backgroundColor: 'white', padding: 20, alignItems: 'center' }} underlayColor="white">
<Text style={{
backgroundColor: 'black', color: 'white', fontSize: 20, fontWeight: 'bold',
height: 50, width: 300, textAlign: 'center', padding: 14
}}>NEXT</Text>
</TouchableHighlight>
</View>
and here is my ListItem view
constructor(props) {
super(props);
this.state = {
selected: false,
text: props.data.text,
value: props.data.value
};
this.choosen = this.choosen.bind(this);
}
choosen(isSelected) {
this.setState({
selected: !isSelected,
});
}
render() {
let backgroundColor = this.state.selected ? "#000000" : "#ffffff";
let fontColor = this.state.selected ? "#ffffff" : "#000000";
return (
<TouchableHighlight selected={this.state.selected} onPress={() => this.choosen(this.state.selected)} underlayColor="black">
<View style={{backgroundColor: backgroundColor, padding: 20 }}>
<Text style={{color: fontColor, fontSize: 20 }}>{this.props.data.text}</Text>
</View>
</TouchableHighlight>
);
}
Now I guess I need a tag or something for my ListItem where I get the value of the item selected. Does anyone has an idea?
Upvotes: 1
Views: 904
Reputation: 6379
If I understand correctly you want the Category List View to store the value of the last selected list item?
The best approach would be to lift the state up. This is how you would do it:
Upvotes: 1