【问题标题】:each child in an array or iterator should have a unique key prop - but i did define a key prop数组或迭代器中的每个孩子都应该有一个唯一的 key prop - 但我确实定义了一个 key prop
【发布时间】:2018-12-22 09:45:24
【问题描述】:

我已经定义了一个关键道具。仍然反应原生是抛出这个警告: 数组或迭代器中的每个子元素都应该有一个唯一的 key prop

            <MapView
                style={styles.map}
            >
                {
                    locations.map((location)=> {
                        if (renderArray[wertwert]){
                            let content;
                            if (location.isARealTimeGPS) {
                                content = <View style={styles.marker}/>
                            } else {
                                content = <LocationInfoComponent location={location} handleLocationClick={this.handleLocationClick}
                                                               locations={locations} infoVisible={infoVisible}
                                />
                            }

                            wertwert = wertwert+1;
                            return (
                                <MapView.Marker
                                    key={JSON.stringify(location.latitude)}
                                    coordinate={location}
                                >
                                    {content}
                                </MapView.Marker>
                            )
                        } else {
                            wertwert = wertwert+1;
                        }
                    })
                }
            </MapView>

【问题讨论】:

  • 您确定JSON.stringify(location.latitude) 正在返回一个唯一值吗?

标签: javascript reactjs react-native


【解决方案1】:

我建议您只使用简单的地图索引而不是其他任何东西:

        <MapView>
            {
                locations.map((location, index)=> {
                    ...
                        return (
                            <MapView.Marker
                                key={index}
                                coordinate={location}
                            >
                                {content}
                            </MapView.Marker>
                        )
                })
            }
        </MapView>

【讨论】:

    【解决方案2】:

    我不确定JSON.stringify(location.latitude) 是否返回唯一值。

    因此,另一种方法是在您的迭代器函数中稍作调整,例如,使用 map 您还可以传递可选参数,该参数始终是唯一的(数组中正在处理的当前元素的索引。),因为它从 0 迭代到你的数组的长度可能是多少。 arr.map((item,index) =&gt; {})

    <MapView
                style={styles.map}
            >
                {
                    // use optional argument (id) as unique value
                    locations.map((location,id)=> {
                        if (renderArray[wertwert]){
                            let content;
                            if (location.isARealTimeGPS) {
                                content = <View style={styles.marker}/>
                            } else {
                                content = <LocationInfoComponent location={location} handleLocationClick={this.handleLocationClick}
                                                               locations={locations} infoVisible={infoVisible}
                                />
                            }
    
                            wertwert = wertwert+1;
                            return (
                                {/* use "id" as a unique value */}
                                <MapView.Marker
                                    key={id}
                                    coordinate={location}
                                >
                                    {content}
                                </MapView.Marker>
                            )
                        } else {
                            wertwert = wertwert+1;
                        }
                    })
                }
            </MapView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-18
      • 2022-08-08
      • 2017-02-23
      • 2017-02-03
      • 2018-04-29
      • 2018-05-22
      • 2017-07-28
      相关资源
      最近更新 更多