React native 节列表未显示

React native 节列表未显示,react-native,React Native,按照React本机教程,我无法获取要显示的数据 执行console.log时,我的数据如下所示: Array [ Object { "data": Object { "address": "8753 2nd Street", "id": "5507", "inspection_date": "2019-03-27", "inspection_time": "07:00:00", "inspection_time_displa

按照React本机教程,我无法获取要显示的数据

执行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>

{item}
renderSectionHeader={({section:{title}}})=>(
{title}
)}
节={this.state.dataSource}
keyExtractor={(项,索引)=>item+index}
/>
这就是我认为正在发生的事情,但我显然错了,因为有些事情没有发生。 循环通过“部分”

renderItem={({item,index,section})=>{item}
我希望上面的内容能够得到“数据”

获取标题:

renderSectionHeader={({section: {title}}) => (
            <Text style={{fontWeight: 'bold'}}>{title}</Text>
          )}
renderSectionHeader={({section:{title}}})=>(
{title}
)}

我希望上面的文章能得到“标题”。我遗漏了什么或做错了什么?

我认为每个节对象中的数据键本身都需要是一个数组。例如:

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}
,从而使节列表能够从title和数据数组中呈现节标题和项目列表


希望有帮助

使用上面的示例,我得到“不变冲突。对象作为React子对象无效(找到:具有键{address,name}的对象)。@krewlobster您不能直接显示整个json。您需要使用renderItem={({item,index,section})=>{item.address}{item.id}”诸如此类。这个答案和你的答案的结合为我解决了这个问题。@shrutigarg
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'} 
    ]
  }
]