反応ネイティブの FlatList でクリックされた要素のインデックスを取得しようとしています。ドキュメントにあるように、renderItem プロップにインデックスを渡しています。私のコードは次のとおりです。
/**
* Goes to the show view of a container
* @param {*} index
*/
showContainer = (index) => {
console.log(index);
}
render() {
return (
<DefaultScrollView style={styles.container}>
<FlatList
data={this.props.containers}
renderItem={(data, index) => (
<ListItem
containerStyle={{borderBottomWidth: 1, borderBottomColor: "#000"}}
key={data.item.id}
leftAvatar={Image}
onPress={() => {this.showContainer(index)}}
rightIcon={{ name: "ios-eye", type: "ionicon" }}
subtitle={
`${data.item.dummy === true? 'Por favor configura tu dispositivo' : 'Contenido actual: '}`
}
subtitleStyle={(data.item.dummy == true)? [styles.configurationText, styles.subtitule] : styles.subtitule}
title={data.item.name}
titleStyle={styles.title}
/>
)}/>
</DefaultScrollView>
);
}
それが機能する唯一の方法は、たとえば: のように数値をインデックスとして
renderItem={(data, index = 0)
渡すことですが、インデックス変数を渡して常に正しいインデックスを取得するにはどうすればよいですか?
前もって感謝します