gpt4 book ai didi

javascript - 如何在 Flat List (React Native) 中交替颜色

转载 作者:数据小太阳 更新时间:2023-10-29 05:59:55 24 4
gpt4 key购买 nike

尝试在 React Natives Flatlist 中替换颜色。我相信我需要 rowID 或类似的东西来做到这一点。这是我到目前为止所得到的:

let colors = ['#123456', '#654321', '#fdecba', '#abcdef'];


<View >
<FlatList style={{backgroundColor: colors[this.index % colors.length]}}

data={this.state.dataSource}
renderItem={({item}) => <Text style={styles.listStyle}>{item.title}, {item.releaseYear}</Text>}
keyExtractor={(item, index) => index}
/>
</View>

有什么想法吗?

最佳答案

renderItem 回调参数有一个属性 index 允许您访问当前行的行索引:

<View >
<FlatList
data={this.state.dataSource}
keyExtractor={(item, index) => index}
renderItem={({item, index}) => (
<View style={{ backgroundColor: colors[index % colors.length] }}>
<Text style={styles.listStyle}>{item.title}, {item.releaseYear}</Text>
</View>
)}
/>
</View>

关于javascript - 如何在 Flat List (React Native) 中交替颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49645671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com