I'm building an app with react native and i try to pass an index to a style property, to have different backgroundColor.
here's my code :
getData(data){
const items = data.map((item, index) =>
<View key={index} style={[styles.forecastItems, styles.forecastItems0]}>
<Text>{item.day}</Text>
<Image source={item.url}/>
<Text>{item.temp}</Text>
</View>
);
return (items)
}
in styles.forecastItem0 i want to have the index instead of 0. i don't find a solution. Thanks for your help
you can use Template literals:
<View key={index} style={[styles.forecastItems, styles[`forecastItems${index}`]]}>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With