我是一个反应原生的新手。我尝试向我的应用程序添加一个平面列表。我有一组这样设计的数据:
["https://hi.com//image.png", //uri
"hello",
"https://hi.com//image2.png",
"welcome",
"https://hi.com//image3.png",
"great",
../..
]
问题是我的图像显示出来了,但右侧的文本实际上是我的 uri 字符串化。
我认为 keyExtractor 有问题:
renderItem =({item}) => {
return(
<View style ={{flex:1, flexDirection:'row'}}>
<Image source ={{uri: item}}/>
<View style ={{flex:1, justifyContent: 'center'}}>
<Text>{item}</Text>
</View>
</View>
)
}
render() {
return (
<View style={styles.mainContainer}>
<FlatList
data= {this.state.dataSource}
keyExtractor={(item,index) => index.toString()}
renderItem= {this.renderItem}
/>
</View>
);
}
您的 renderItem 函数循环遍历数组的每个元素,不确定它是您的数据类型的最佳选择,也许尝试使用这样的东西代替
const data = [{uri: 'https://link.io', text: 'hello'},{uri: 'http://anotherlink.co', text: 'bye'}]
然后在您的 renderItem 功能传递数据中:
<Image source ={{uri: item.uri}}/>
<Text>{item.text}</Text>
另一方面,如果您需要保留平面数组,也许可以编写一些函数,其模数将索引除以 2,然后从那里得到什么,但不知道为什么除了 codewars challange 之外,您还需要它;) 祝你好运, 希望这可以帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句