Reactjs 如何修复';列表中的每个孩子都应该有一个唯一的;“关键”;道具和#x27;

Reactjs 如何修复';列表中的每个孩子都应该有一个唯一的;“关键”;道具和#x27;,reactjs,Reactjs,我正在从JSON api中提取数据。我在map函数生成的unique元素中指定了一个键,但是我无法理解为什么我仍然看到这个警告。我需要在哪里分配密钥 import React from 'react'; import PokeCell from './PokeCell'; import './styles/PokeList.css'; const PokeList = (props) => { return ( <section className="poke-list"

我正在从JSON api中提取数据。我在map函数生成的unique元素中指定了一个键,但是我无法理解为什么我仍然看到这个警告。我需要在哪里分配密钥

import React from 'react';
import PokeCell from './PokeCell';
import './styles/PokeList.css';

const PokeList = (props) => {

return (
    <section className="poke-list">
        {props.pokemon.map((pokemon) => (
            <PokeCell 
                key={pokemon.id}
                id={pokemon.id}
                name={pokemon.name}
                image={pokemon.image}
            />
         ))}
    </section>
 )
}

export default PokeList;

index.js:1375 Warning: Each child in a list should have a unique "key" 
prop.

Check the render method of `PokeList`
    in PokeCell (at PokeList.js:10)
    in PokeList (at App.js:24)
    in div (at App.js:20)
    in App (at src/index.js:7)
从“React”导入React;
从“./PokeCell”导入PokeCell;
导入“./styles/PokeList.css”;
const PokeList=(道具)=>{
返回(
{props.pokemon.map((pokemon)=>(
))}
)
}
导出默认PokeList;
js:1375警告:列表中的每个子级都应该有一个唯一的“键”
道具
检查“PokeList”的渲染方法`
在PokeCell(位于PokeList.js:10)
在PokeList中(App.js:24)
在div中(在App.js:20中)
应用程序内(位于src/index.js:7)

您可以使用数组
索引来拥有唯一键

<section className="poke-list">
    {props.pokemon.map((pokemon, index) => (
        <PokeCell 
            key={pokemon.id + index}
            id={pokemon.id}
            name={pokemon.name}
            image={pokemon.image}
        />
     ))}
</section>

{props.pokemon.map((pokemon,index)=>(
))}

您可以使用数组
索引来拥有唯一键

<section className="poke-list">
    {props.pokemon.map((pokemon, index) => (
        <PokeCell 
            key={pokemon.id + index}
            id={pokemon.id}
            name={pokemon.name}
            image={pokemon.image}
        />
     ))}
</section>

{props.pokemon.map((pokemon,index)=>(
))}

您必须确保每个id都是唯一的,以使react能够正确更新组件

您不应该将索引用作键值,因为从列表中删除项目或对其重新排序可能会导致错误


您必须确保每个id在列表的所有可能配置(添加、删除等)中都是唯一的,以防止不必要的重新提交


您应该保留该实现并更改保护数据的方式,以提供唯一的id。

您必须确保每个id都是唯一的,以使react能够正确更新组件

您不应该将索引用作键值,因为从列表中删除项目或对其重新排序可能会导致错误


您必须确保每个id在列表的所有可能配置(添加、删除等)中都是唯一的,以防止不必要的重新提交


您应该保留该实现,并更改保护数据的方式以提供唯一ID。

您将密钥放在了正确的位置。。。就在你回电话之前,你可以
clg
props.pokemon的id吗?pokemon.id有有效值,对吗?你把钥匙放在了正确的位置。。。就在你回电话之前,你可以
clg
道具的id。口袋妖怪?口袋妖怪.id有有效值,对吗?这很有效!非常感谢你!我想pokemond.id并不像我想象的那样独特。这很有效!非常感谢你!我想pokemond.id并不像我想象的那样独一无二。