简体   繁体   English

列表中的每个孩子都应该有一个唯一的“关键”道具。 我做到了,但仍然看到这个错误

[英]Each child in a list should have a unique "key" prop. I made it, but still see this error

I know about key prop, so i made it in listitem component我知道 key prop,所以我在 listitem 组件中制作了它

const ListItem = ({item}) => {
const {result, time, id} = item;
return(
    <li key={id} className='list__item'>
        <span className='item__result'>{result} cps</span>
        <span className='item__date'>{time}</span>
        <button className='item__delete'>delete</button>
    </li>
)}

And here is component, where I use it这是组件,我在哪里使用它

const Leadboard = () => {
const [data, setData] = useState([{result:'5,63', time:'08.06.2022', id:  (new Date()).toString(16)}, {result:'5,63', time:'08.06.2022', id: +(new Date() - 1)}, {result:'5,63', time:'08.06.2022', id: +(new Date() - 2)}]);

let elements=data.map(item => {
    return (
        <>
            <ListItem item={item} />
        </>
    )
});

return(
    <div className='app-leadboard'>
        <span className='app-leadboard__title'>Your's best results:</span>
        <ol className='app-leadboard__list' type='1'>
            {elements}
        </ol>
    </div>
)}

But after render I still see "key prop" error但渲染后我仍然看到“关键道具”错误

I spent so much time on this, but can't understand what's wrong.我花了很多时间在这上面,但不明白出了什么问题。 So smb, help pls with it所以 smb,请帮忙

You've got the wrong list.你有错误的清单。 It's the <ListItem> components that need the key. <ListItem>组件需要密钥。 (And you can get rid of the react fragments around them because they are pointless). (并且您可以摆脱它们周围的反应片段,因为它们毫无意义)。

React first accesses the empty bracket ( <> </> ) before accessing the key attribute in your child component. React 在访问子组件中的key属性之前首先访问空括号( <> </> )。

So you need to either所以你需要要么

  1. Make use of the empty brackets and pass the key attribute to it利用空括号并将key属性传递给它
// Use React.Fragment 

  let elements=data.map(item => { return 
  (
   <React.Fragment key={item.id}>
 <ListItem item={item} />
</React.Fragment>
)
});

and remove the key in the child ( ListItem ) component并删除子( ListItem )组件中的键

ListItem.js
 <li 
 /*  Remove this
key={id} 
*/

  className='list__item'>

OR或者

Get rid of the empty brackets ( <> </> ) and reference the child's component directly.去掉空括号( <> </> )并直接引用子组件。

let elements=data.map(item => {
return (<ListItem item={item} />)
});

and leave the key attribute in the child component并将key属性留在子组件中

More on React Fragment.更多关于反应片段。 React Official Docs React 官方文档

暂无
暂无

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

相关问题 列表中的每个孩子都应该有一个独特的“关键”道具。 但我没有调用任何组件 - Each child in a list should have a unique "key" prop. but i'm not calling any component 列表中的每个孩子都应该有一个唯一的“关键”道具。 即使我给了一把钥匙,而且它是独一无二的 - Each child in a list should have a unique “key” prop. Even though i am giving a key and its a unique one 警告:列表中的每个孩子都应该有一个唯一的“key”道具。 即使已经设置了密钥 - Warning: Each child in a list should have a unique "key" prop. Even after setting the key already 正在渲染对象数组的数组,不断得到“警告:列表中的每个孩子都应该有一个唯一的“键”道具。 - Was rendering arrays of arrays of objects, keep getting “Warning: Each child in a list should have a unique ”key“ prop.” 警告:列表中的每个孩子都应该有一个唯一的“关键”道具。 检查 `RenderComments` 的渲染方法 - Warning: Each child in a list should have a unique "key" prop. Check the render method of `RenderComments` 警告:列表中的每个孩子都应该有一个唯一的“关键”道具。 mapStateToProps - Warning: Each child in a list should have a unique "key" prop. mapStateToProps 列表中的每个孩子都应该有一个唯一的“key”道具。 检查`Form`的渲染方法 - Each child in a list should have a unique “key” prop. Check the render method of `Form` 收到警告“警告:列表中的每个孩子都应该有一个唯一的”key“道具。” 当我的组件渲染时 - Getting warning “Warning: Each child in a list should have a unique ”key“ prop.” when my component render 警告:列表中的每个孩子都应该有一个唯一的“key”道具。 - ReactJS - Warning: Each child in a list should have a unique "key" prop. - ReactJS 警告:列表中的每个孩子都应该有一个唯一的“关键”道具。 如何解决这个问题? - Warning: Each child in a list should have a unique "key" prop. how to fix this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM