正在选择来自设备相机的多张照片并将其传递给平面列表组件,我在底部有文本输入,可以在每张照片下方添加文本。我想获取作为道具传递的文本输入的索引,但它说找不到变量索引。
代码:
const Flatlistitems = (props) => {
return(
<FlatList
style={{
flexDirection: 'row',
marginRight: 10,
marginLeft: 5,
flex: 1,
}}
data={props.data}
horizontal={true}
pagingEnabled={true}
snapToInterval={CARD_WIDTH}
scrollEventThrottle={16}
showsHorizontalScrollIndicator={false}
decelerationRate={'fast'}
snapToAlignment={'start'}
contentContainerStyle={{ justifyContent: 'center', Flex: 1 }}
keyExtractor={(item, index) => index.toString()}
renderItem={({ item, index }) => (
<View
style={{
justifyContent: 'center',
marginRight: 10,
marginLeft: 30,
alignContent: 'space-around',
Flex: 1,
}}
key={index}
>
<Image
source={item}
style={{
height: height / 3.1,
width: CARD_WIDTH,
backgroundColor: 'gray',
}}
/>
<View style={{ flex: 1 }}>
<TextInput
maxLength={100}
autoFocus={true}
contextMenuHidden={true}
underlineColorAndroid="transparent"
returnKeyType="next"
textAlignVertical={'top'}
style={{
borderColor: 'black',
borderBottomWidth: StyleSheet.hairlineWidth,
height: 40,
color: 'black',
width: CARD_WIDTH,
}}
multiline={false}
numberOfLines={1}
placeholder="Add a caption"
placeholderTextColor="black"
onChangeText={props.onChangeText.bind(null, index)}
value={props.value.bind(null, index)}
/>
</View>
</View>
)}
/>
)
}
也试过这个:
onChangeText={props.onChangeText[index]}
value={props.value[index]}
用法:
<Flatlistitems
data={photos}
onChangeText={(text) => {
textInputs = text;
this.setState({
textInputs,
});
}}
value={textInputs}
/>