【问题标题】:React Native return function doesn't properly add numbersReact Native 返回函数没有正确添加数字
【发布时间】:2021-07-15 22:44:20
【问题描述】:

我正在为一个项目使用 React Native 和 Firebase firestore。

在 Firestore 中,文档有一个名为 teammates 的字段。在该字段中,有一个数组,里面有映射(嵌套对象),其中包含以下字段:key(int, 从 0 开始)、名称、字段。

问题是,我想在 View key+1 中返回,以便 ID 从 1 开始而不是 0。这适用于数组中的第一个映射,对于键 0,但在 firebase 中从 key = 1 开始,它会返回ID 11 而不是 ID 1+1(2)

<FlatList
                
       
       data={post}
       
       renderItem={({ item }) => (
          
          <View>
                   {item.teammates.map((item) => (
                       <View>
                       <Text>ID: {item.key + 1}</Text>
                       <Text>Name: {item.name}</Text>
                       </View>
                       
                   ))}


           </View>
           

       )}

   />

【问题讨论】:

  • 听起来 item.key 是字符串而不是数字,请尝试将其包裹在 parseInt()
  • 非常感谢!那救了我!但根据 firestore 数据库,item.key 是一个数字。奇怪
  • 也考虑发布答案,谢谢

标签: reactjs react-native react-native-flatlist


【解决方案1】:

如果item.key是一个字符串,你需要先把它解析成一个number,然后你可以添加+1。类似的东西

<Text>ID: {parseInt(item.key, 10) + 1}</Text>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 2019-11-23
    • 1970-01-01
    相关资源
    最近更新 更多