我正在尝试从api中提取json的FlatList,但是我一直收到这个错误: Warning: Each child in an array or iterator should have a unique "key" prop.Check the render method of `VirtualizedList`. 相关代码: FlatList data={this
Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `VirtualizedList`.
相关代码:
<FlatList
data={this.props.tunes}
keyExtractor={(item, index) => item.id}
renderItem={({item}) => {
<Item
key={item.id}
title={item.title}
composer={item.composer}
year={item.year}
/>
}}
/>
我确定有一个简单的解决办法,但经过几天尝试不同的事情,我还没有找到它.谢谢你的帮助!
看起来你需要在keyExtractor中编写item.id时将密钥更改为id,并确保你有id并且每个组件的ID都不同:<FlatList
data={this.props.tunes}
keyExtractor={(item, index) => item.id}
renderItem={({item}) => {
<Item
id={item.id} //instead of key
title={item.title}
composer={item.composer}
year={item.year}
/>
}}
/>
或者,如果您没有唯一键,请使用keyExtractor = {(item,index)=>指数}
