React Native:uri在平面列表中多次呈现

达纳索

我是一个反应原生的新手。我尝试向我的应用程序添加一个平面列表。我有一组这样设计的数据:

   ["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>       
  );
}
wijuwiju

您的 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

React Native:平面列表未呈现

如何在 React Native 中根据平面列表中的列呈现数据

React Native Firestore 集合不呈现平面列表

React Native中的函数内部的平面列表

React Native 中的反向平面列表

在React Native中组件未在平面列表中返回

在 React Native 中更新平面列表中的参数

传递平面列表将“项目”呈现给函数。React Native(有点js问题)

如何在react-native上刷新/重新呈现平面列表?

React Native 中的可点击平面列表项

React Native 中的自定义平面列表项

想在React Native中自动滚动平面列表

React Native:在平面列表中使用挂钩

React Native获取平面列表索引

在React Native中单击项目时,如何关闭模式列表并从平面列表返回值?

在 React Native 中,如何从 json 数组动态获取平面列表中的键

在 react-native 中展开水平平面列表中的卡片

无法在 React-Native 中显示没有键/id 的值数组中的平面列表

React Native中的URI与URL

在react-native中实现对两个可拖动平面列表的搜索

尝试在React Native中在平面列表内声明变量时出现意外令牌

React Native:将数据也从平面列表引入到已分配的模式中

如何在React Native中从绑定到平面列表的不同组件调用函数

如何在 React Native 的平面列表中更改所選項目的顏色?

如何在 React-Native 中处理平面列表项的状态

如何通过在react native中单击一个平面列表图像来显示特定图像?

如何在react native(没有json)中添加带有平面列表的搜索栏?

在 setInterval 期间,React Native 可触摸不透明度在平面列表中失败

如何在 React Native 中创建带有标题的动态平面列表模态库?