<SectionList
sections={[{ data: [1, 2] }, { data: [3, 4] }]}
renderItem={({ item, index }) => ...}
renderSectionHeader={({ section, index }, i) => {
console.log(index, i); // both undefined
}}
/>
我想在renderSectionHeader
中获取该部分的索引
例如。 index
为section.data
时[1, 2]
应为0; section.data
为[3, 4]
时为{1}。
除了将索引添加到sections
数据?
答案 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 />
}}
希望这会有所帮助。