react-native - 部分列表不显示
问题描述
遵循 React-Native 教程,我无法获得要显示的数据。
当我执行 console.log 时,我的数据如下所示:
Array [
Object {
"data": Object {
"address": "8753 2nd Street",
"id": "5507",
"inspection_date": "2019-03-27",
"inspection_time": "07:00:00",
"inspection_time_display": "07.00 AM",
"inspector": "Frank",
},
"key": "5507",
"title": "8753 2nd Street",
},
Object {
"data": Object {
"address": "11445 Paramount ave ",
"id": "5505",
"inspection_date": "2019-03-23",
"inspection_time": "10:30:00",
"inspection_time_display": "10.30 AM",
"inspector": "Fabian Hernandez",
},
"key": "5505",
"title": "11445 Paramount ave ",
},
]
如大多数教程中所示,我有“数据”和“标题”部分。
我的组件是这样的:
<Container>
<Header />
<Content>
<SectionList
renderItem={({item, index, section}) => <Text key={index}>{item}</Text>}
renderSectionHeader={({section: {title}}) => (
<Text style={{fontWeight: 'bold'}}>{title}</Text>
)}
sections={this.state.dataSource}
keyExtractor={(item, index) => item + index}
/>
</Content>
</Container>
这就是我认为正在发生的事情,但我显然错了,因为有些事情没有加起来。循环遍历“部分”
renderItem={({item, index, section}) => <Text key={index}>{item}</Text>}
我期待上面的这个能得到“数据”。
获取标题:
renderSectionHeader={({section: {title}}) => (
<Text style={{fontWeight: 'bold'}}>{title}</Text>
)}
我期待上面的这个得到“标题”。我错过了什么或做错了什么?
解决方案
我相信每个部分对象中的数据键本身都需要是一个数组。例子:
const mySectionedData = [
{
title: 'section 1',
data: [
{address: '123 street', name: 'me'},
{address: '456 street', name: 'you}
]
},
{
title: 'section 2',
data: [
{address: '789 street', name: 'us'}
]
}
]
然后,这使您可以{title, data}
从每个部分访问,这允许部分列表然后从标题呈现您的部分标题,并从数据数组中呈现项目列表。
希望有帮助!
推荐阅读
- sql - 如何使用 Python 将 Dataframe 放入 SQL where 子句中?
- spring - Spring boot 找不到模板位置调试模式
- c++ - 当我关闭文件资源管理器时,我的 C++ 程序被删除
- javascript - 为什么我的这个 React 代码会出现无限循环?
- javascript - 从 json 文件读取时,如何返回对象而不是数组中的对象?
- kubernetes - 504 使用多个复制的 pod 访问 Web 服务。单吊舱没有错误
- dbt - dbt:模型“bar”依赖于模型“foo”,该模型未找到或被禁用
- python - Python to .exe with pyinstaller not re-ssized 问题
- parallel-processing - 如何指定芹菜工人可以接受的任务的批量大小?
- sql - 如何获取特定状态或 ID 的总和并在 laravel 中加入 3 个表