How to map attributes array (which is the sub-array of products)into the picker. Here is my JSON data:
{ "products": [ { "productid": "5466", "name": "Cashew", "description": "", "attributes": [ { "product_id": "5466", "attribute_name": "500 grm", "attribute_price": "500" }, { "product_id": "5466", "attribute_name": "250 grm", "attribute_price": "250" } ] } ] }
here is my code:
renderProductsCart = ({ item }) => {
return (
<View style={{ width: "46%", marginLeft: 10, marginTop: 10 }}>
<Card
elevation={2}>
<Card.Cover source={{ uri: item.image }} />
<Text style={{ marginLeft: 5 }}>{item.name}</Text>
<Text> ₹: {item.attribute_price}/- </Text>
<Picker
selectedValue={this.state.PickerValueHolder}
onValueChange={(itemValue, itemIndex) => this.setState({ PickerValueHolder: itemValue })} >
{this.state.data.products.attributes.map((item, key) => (
<Picker.Item label={item.attribute_name} value={item.attribute_name} key={item} />)
)}
</Picker>
<View style={{ flexDirection: "row" }}>
<Card.Actions>
<Button
theme={{ colors: { primary: 'black' } }}
onPress={() => console.log("press")}>View More</Button>
<Button
theme={{ colors: { primary: 'black' } }}
onPress={() => console.log("press")}>Cart</Button>
</Card.Actions>
</View>
</Card>
</View>
)
}
<FlatList
data={this.state.data.products}
renderItem={this.renderProductsCart}
keyExtractor={(item, index) => index.toString()}
numColumns={2}
/>
please help me thanks in advance
I have used the Functional component here but you can use the Class-based component and use it that way too: Output:
Full Example Code:
Working Expo Snack Demo