javascript - 如何在 React 本机 FlatList 中加载两个状态,可以吗?
问题描述
我在下面有两个状态。
state={
title=['abc','def','ghi']; //data for-example.
Subheading=['aaa','bbb','ccc']; //data for-example.
}
我想通过 FlatList 跟踪输出。
abc
aaa
def
bbb
ghi
ccc
我尝试过的代码只能加载标题状态,但如何在文本组件中加载子标题和标题。
我的代码类似:
<FlatList
style={{ flex: 1, padding: 9 }}
data={Object.assign(this.state.title)}
keyExtractor={(key, index) => key + index}
renderItem={(itemData) => {
return <Card.Title
title={itemData.item}
subtitle={<Text>//how to load subheading</Text>}
onPress={() => console.log("Pressed")}>
/>
}
}
/>
解决方案
有几种方法可以做到这一点,但我认为更简洁的方法是将数组合并到这种形式的对象中[{ title: 'aaa', subtitle: 'abc' }]
const data = title.map( (item,index) => {
return { title: item, subtitle: Subheading[index] }
}
然后在您的平面列表中,您可以使用item.title
和item.subtitle
从程序员的角度来看,我将始终以结构化和可读的方式组织我的数据,以便最终我的渲染代码也将是可读的(而不是多个数组 [index])
推荐阅读
- python - 如何对 bitbucket 云 API 进行身份验证,然后在身份验证后使用它们?
- java - 从多个地方在 JPanel 上绘图
- php - Laravel 数据透视表 - 保存来自其他列的数据
- java - 无效或有问题的 jar 文件
- swift - 尽管 zPosition = 1,但无法强制子视图位于最前面
- python - 使用 Chrome 驱动程序时出错 - 未捕获的 ReferenceError:未定义 showDate
- java - 将 java 代理添加到 wildfly10.1.Final 服务器
- python - 总结熊猫数据框中的某些行组
- laravel - Laravel 队列执行两次
- java - 将 ClickListener 添加到 RecyclerView 实时数据库