简体   繁体   English

有一种方法可以解决“警告:列表中的每个孩子都应该有一个唯一的“密钥”?

[英]there is a way to fix "Warning: Each child in a list should have a unique "key"?

Is there a way to fix this warning?有没有办法解决这个警告?

Warning: Each child in a list should have a unique "key警告:列表中的每个孩子都应该有一个唯一的“键”

I got this warning every time and don't understand how to fix it.我每次都收到此警告,但不知道如何解决。 I try to fix it but i realize that something wrong in my way .我试图修复它,但我意识到我的方式出了问题。 hope to understand whats wrong because its so annoying.希望了解什么是错的,因为它太烦人了。

import React, { Component } from 'react';
import { View, Text, StyleSheet, ActivityIndicator, Platform, FlatList, Dimensions, Image } from 'react-native';
import { HeaderButtons, Item } from 'react-navigation-header-buttons'
import HeaderButton from '../components/HeaderButton';
import axios from 'axios';


const { width, height } = Dimensions.get('window');

export default class PlacesListScreen extends Component {
    constructor(props) {
        super(props);
        this.state = { isLoading: true, data: [] };
    }

    componentDidMount() {
        axios.get('https://rallycoding.herokuapp.com/api/music_albums')
            .then(res => {
                this.setState({
                    isLoading: false,
                    data: res.data,
                })
                console.log(res.data);
            })
    }

    renderItem(item) {
        const { title, artist } = item.item;
        return (
            <View style={styles.itemView}>
                <View style={styles.imgContainer}>
                    {/* <Image style={styles.imageStyle}
                        source={{ uri: image }}
                    /> */}
                </View>

                <View style={styles.itemInfo}>
                    <Text style={styles.name}>
                        {title+ ' ' + artist}
                    </Text>
                    <Text style={styles.vertical} numberOfLines={1}>{title} |</Text>
                </View>
            </View>
        );
    }

    render() {
        if (this.state.isLoading) {
            return (
                <View style={{ flex: 1, padding: 20 }}>
                    <Text style={{ alignSelf: 'center', fontWeight: 'bold', fontSize: 20 }}>loading...</Text>
                    <ActivityIndicator />
                </View>
            )
        }


        return (
                <View style={styles.container}>
                    <FlatList
                        data={this.state.data}
                        renderItem={this.renderItem.bind(this)}
                        keyExtractor={item => item.id}
                    />
                </View>
            );
        }
    }

Hope that you understand my problem and how can I fix it.希望您了解我的问题以及如何解决。 the example code above shows my try to get some data from API, but it returns a warning every time about each child in a list should have a unique "key".上面的示例代码显示了我尝试从 API 获取一些数据的尝试,但它每次都会返回一个警告,即列表中的每个子项都应该有一个唯一的“键”。

console.log the key of each item. console.log 每个项目的键。 It may be possible u have the same id for some items?对于某些项目,您可能有相同的 ID 吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:列表中的每个孩子都应该有一个唯一的“关键”道具。 如何解决这个问题? - Warning: Each child in a list should have a unique "key" prop. how to fix this? 警告列表中的每个孩子都应该有一个唯一的“关键”道具 - Warning Each child in a list should have a unique "key" prop 像这样的警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - warning like : Each child in a list should have a unique "key" prop 反应警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - React Warning: Each child in a list should have a unique "key" prop 警告:列表中的每个孩子都应该有一个唯一的“key”道具 - Warning: Each child in a list should have a unique “key” prop 错误:警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - ERR : Warning: Each child in a list should have a unique "key" prop 修复警告 ///列表中的每个孩子都应该有一个唯一的“键”道具 - fixing warning ///Each child in a list should have a unique "key" prop Reactjs:警告列表中的每个孩子都应该有一个唯一的“关键”道具 - Reactjs: Warning Each child in a list should have a unique "key" prop 警告:列表中的每个孩子都应该有一个唯一的“键”道具来切换 - Warning: Each child in a list should have a unique "key" prop for switch 错误警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - ERROR Warning: Each child in a list should have a unique "key" prop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM