react-native - 添加动态背景颜色以从状态反应本机查看
问题描述
我有一个父视图容器,它具有三个具有不同背景颜色的子视图。我想为视图设置的背景颜色存储为 state 中的值。
我需要实现这样的目标:
这是我的代码片段:
this.state={
myData:[],
color:["#27AE60", "#3498DB", "#E67E22","#E74C3C","#DAF7A6"]
}
render(){
const oem_color = this.state.color.map((i,e) => {
return(
<View key={i} style={{width:15, height:15, borderRadius:50, backgroundColor: {i.color}}} />
)
})
return (
<View>
</View>
)
请帮忙解决。
解决方案
另一种方法是使用 Flatlist 只需在 flatlist 的数据中传递此状态
<FlatList
data={this.state.color}
renderItem={(item, index) => {
return (
<View
key={index}
style={{
width: 15,
height: 15,
borderRadius: 50,
backgroundColor: item,
}}
/>
)}}
推荐阅读
- python - Django (Password_change) 不起作用 url
- android - 使用房间数据库查询在产品之间进行搜索
- jquery - 防止动态创建的 DOM 元素上的内存泄漏
- resharper - 有没有办法告诉 R# 在我运行 aa 方法后,某些类变量不会为空
- css - 删除shinydashboardPlus中右侧边栏宽度更改时出现的多余空间
- php - 我的 WP_Query 不会返回超过 9 个项目
- css - Bootstrap 导航栏切换按钮不起作用 - 显示但不打开导航栏(Angular)
- c# - 连接被拒绝 Kubernetes
- networking - .NET Core PrefixOrigin 逻辑
- python - 可变步长抽取函数的优化