获取 SectionList 项的索引 React-Native

Get index of SectionList Item React-Native

我在从 SectionList React-Native 获取 header 项的索引时遇到问题。通过按 header,我试图获取项目的索引,然后将其传递给函数。我尝试了很多东西,但没有运气。有什么建议么。谢谢

我想按 3-30pm 到 return 索引 0 我可以按 Lucian 哪个 return 是我 0 我的想法是通过获取 header 索引,我可以使用数组从列表中删除一个项目。

            <SectionList style = {styles.itemSquare}

        renderItem = {({item, index, section}) =>
            < Text style={styles.SectionListItemStyle} key = {index}
            onPress={this.GetSectionListItem.bind(this, this.state.slotkeys[index])}> {item}
            < /Text>}

            renderSectionHeader = {({section: {title}, index}) => (
                  <TouchableHighlight >
                    <View>
                      <Text style={styles.SectionHeaderStyle}
                      onPress={this.GetSectionListItem.bind(this, index)}
                      > {title}
                      <Text style={styles.SectionHeaderCancel} > {index} < /Text>
                      </Text>
                    </View>
                  </TouchableHighlight>
                )
            }

          sections = {this.state.slots.map(({ time, chosen_user, name, chosen_syllabud, user_id }) =>
          ({ title: time, data: [[chosen_user], [chosen_syllabud], [user_id]], index:1 }))}

          keyExtractor = {(item, index) => item + index}
          />

renderSectionHeader prop 在调用时不会接收索引作为参数,但您可以修复 sections prop 以通过 map 正确传递索引:

  sections = {this.state.slots.map(({ time, chosen_user, name, chosen_syllabud, user_id }, index) =>
      ({ title: time, data: [[chosen_user], [chosen_syllabud], [user_id]], index }))}

然后在您的 renderSectionHeader 上,您可以访问该部分内的索引:

renderSectionHeader = {({section: {title, index}}) => (
              <TouchableHighlight >
                <View>
                  <Text style={styles.SectionHeaderStyle}
                  onPress={this.GetSectionListItem.bind(this, index)}
                  > {title}
                  <Text style={styles.SectionHeaderCancel} > {index} < /Text>
                  </Text>
                </View>
              </TouchableHighlight>
            )
        }