I want to change the background color of flatlist item according to array ans but not able to change background-color of flatlist only 1st item background color is changed
const ans = [1, 3, 5];
const [dataSource, setDataSource] = useState([]);
useEffect(() => {
let items = Array.apply(null, Array(5)).map((v, i) => {
return {
id: i + 1,
};
});
setDataSource(items);
}, []);
const renderItem = ({ item, index }) => {
const backgroundColor = item.id == ans[index] ? '#00b300' : "#F5FCFF"; \\ here is main code
return (
<TouchableWithoutFeedback onPress={() => getItem(item)}>
<View style={{ ...styles.viewStyle, backgroundColor: backgroundColor, flex: 1 / 5,
height: 40, }} >
<Text style={{ ...styles.cardView_Text, color: '#000' }} > {item.id} </Text>
</View>
</TouchableWithoutFeedback>
);
};
const getItem = (item) => {
// alert('Id: ' + item.id);
};
<FlatList
data={dataSource}
showsHorizontalScrollIndicator={false}
renderItem={renderItem}
keyExtractor={(item, index) => index.toString()}
extraData={selectedId}
numColumns={5}
initialNumToRender={2}
maxToRenderPerBatch={1}
windowSize={7}
/>