有关react-native组件键的警告

时间:2017-12-06 08:31:34

标签: react-native

我很反应原生。我遇到了动态渲染组件的问题。

我正在使用滑动组件。如果只有一张牌要呈现,我会收到警告:

"警告:遇到两个拥有相同密钥的孩子0。密钥应该是唯一的,以便组件在更新时保持其身份。非唯一键可能导致子项被复制和/或省略 - 行为不受支持,并且可能在将来的版本中更改。"

当要渲染多张牌时,一切正常,但当只有一张牌要渲染时,我会收到警告。

我读到了这个:Understanding unique keys for array children in React.js

但它对我来说并不合理:(

我也在这里发布了我的问题:

https://github.com/alexbrillant/react-native-deck-swiper/issues/90

任何帮助将不胜感激!

由于

编辑:添加代码。

我的代码截图位于上面的链接中,但我认为最相关的代码是:

cards.push(<MyPetCard
petId={responseJson[x].petId}
petName={responseJson[x].name}
breed={responseJson[x].breed}
gender={responseJson[x].gender}
category={responseJson[x].category}
description={responseJson[x].description}
birthday={responseJson[x].birthday}
imageInfo={image}
petAge={responseJson[x].age}
distance={petDistance}
significantValue={responseJson[x].significant}
/>);

render method looks like this

1 个答案:

答案 0 :(得分:0)

仅供参考,我的问题特别针对react-native-deck-swiper 如果传递给swiper的牌阵列只包含一个元素,我就会遇到警告。

似乎swiper组件正在准备下一张要渲染的卡片,但由于这些卡片只有一个项目,因此组件键是重复的。

我也在这里发布了答案:

https://github.com/alexbrillant/react-native-deck-swiper/issues/90

希望它有所帮助!