SectionList获取renderSectionHeader中的section index

时间:2018-05-12 13:04:49

标签: react-native react-native-sectionlist

<SectionList
  sections={[{ data: [1, 2] }, { data: [3, 4] }]}
  renderItem={({ item, index }) => ...}
  renderSectionHeader={({ section, index }, i) => {
    console.log(index, i); // both undefined
  }}
/>

我想在renderSectionHeader中获取该部分的索引 例如。 indexsection.data[1, 2]应为0; section.data[3, 4]时为{1}。

除了将索引添加到sections数据?

之外,我该如何做到这一点?

2 个答案:

答案 0 :(得分:11)

在本地反应原则的SectionList中没有renderSectionHeader的部分索引,但你可以为你的部分添加索引支柱

 sections={[{ data: [1, 2], index:0 }, { data: [3, 4], index:1 }]}

然后像这样访问renderSectionHeader中的索引

 renderSectionHeader={({section}) => {
     console.log(section.index); 
 }}

答案 1 :(得分:4)

我知道已经晚了,但也许它可以帮助某人。更好的方法是从传递给SectionList组件的数组中获取节的索引。 例如,假设您将dataList作为数组传递给sections={dataList}节,则可以按以下方式获取索引:

renderSectionHeader={({ section }) => {
    const index = dataList.indexOf(section)
    // Here you have the index
    return <View />
}}

希望这会有所帮助。