简体   繁体   English

如何使用索引并摆脱 React 警告:列表中的每个孩子都应该有一个唯一的“关键”道具?

[英]How to use index and get rid of React Warning: Each child in a list should have a unique "key" prop?

Hello I'm facing this error, question is how to propertly pass index in this component?您好,我遇到了这个错误,问题是如何在此组件中正确传递索引?

Error:错误:

react-jsx-dev-runtime.development.js:117 Warning: Each child in a list should have a unique "key" prop.

Here is the component's code fragment:这是组件的代码片段:

                            {calculatorScreenshots.map((imgUrl: any, index) => {
                                return (
                                    <Zoom>
                                        {props.projectName == 'Calculator' && (
                                            <CardMedia
                                                key={index}
                                                component="img"
                                                height="200"
                                                alt="project picture"
                                                image={imgUrl}
                                            />
                                        )}
                                    </Zoom>
                                );
                            })}

The top-level <Zoom> component needs a key attribute as well.顶级<Zoom>组件也需要一个key属性。

You should be placing the key prop at the first element您应该将 key 道具放在第一个元素

                        {calculatorScreenshots.map((imgUrl: any, index) => {
                            return (
                                <Zoom key={index}>
                                    {props.projectName == 'Calculator' && (
                                        <CardMedia
                                  
                                            component="img"
                                            height="200"
                                            alt="project picture"
                                            image={imgUrl}
                                        />
                                    )}
                                </Zoom>
                            );
                        })}

You need to add a key props to the element you rendering with map() .您需要向使用map()渲染的元素添加key道具。

Keys help React identify which items have changed, are added, or are removed.键帮助 React 识别哪些项目已更改、添加或删除。 Keys should be given to the elements inside the array to give the elements a stable identity应该为数组内的元素赋予键,以赋予元素稳定的标识

In your case, you forgot to add a key={imgUrl} to <Zoom></Zoom> element whereas it's the first element rendered with map()在您的情况下,您忘记将key={imgUrl}添加到<Zoom></Zoom>元素,而它是使用map()呈现的第一个元素

To turn off this warning, here the solution:要关闭此警告,这里是解决方案:

{calculatorScreenshots.map((imgUrl: String, index) => {
    return (
        <Zoom key={imageUrl}>
            {props.projectName == 'Calculator' && (
                <CardMedia
                  component="img"
                  height="200"
                  alt="project picture"
                  image={imgUrl}
                />
             )}
       </Zoom>
   )})}

You were almost there, you simply put the key props for the second mapped element, not for the first one <Zoom></Zoom> .你几乎就在那里,你只需为第二个映射元素放置key道具,而不是为第一个<Zoom></Zoom>

Happy coding快乐编码


Some good pieces of information:一些不错的信息:

The best way to pick a key is to use a string that uniquely identifies a list item among its siblings.选择键的最佳方法是使用一个字符串,该字符串在其兄弟项中唯一标识一个列表项。 Most often you would use IDs from your data as keys大多数情况下,您会使用数据中的 ID 作为键

When you don't have stable IDs for rendered items, you may use the item index as a key as a last resort当您没有稳定的呈现项目 ID 时,您可以使用项目索引作为最后的手段

We don't recommend using indexes for keys if the order of items may change.如果项目的顺序可能会改变,我们不建议对键使用索引。 This can negatively impact performance and may cause issues with component state. Check out Robin Pokorny's article for an in-depth explanation on the negative impacts of using an index as a key.这会对性能产生负面影响,并可能导致组件 state 出现问题。查看 Robin Pokorny 的文章,深入了解使用索引作为键的负面影响。 If you choose not to assign an explicit key to list items then React will default to using indexes as keys.如果您选择不为列表项分配显式键,那么 React 将默认使用索引作为键。

You can find more information here:您可以在这里找到更多信息:

List and Keys with React使用 React 的列表和键

Index as a key is an anti-pattern by Robin Pokorny 索引作为键是 Robin Pokorny 的反模式

Why keys are necessary for React? 为什么 React 需要键?

You should write key in <Zoom /> component rather than in <CardMedia /> like this您应该在<Zoom />组件中而不是像这样在<CardMedia />中编写key

<Zoom key={index} />

Note: Always write key to first item component/element you are rendering here it is <Zoom /> .注意:始终将键写入您在此处渲染的第一个项目组件/元素,它是<Zoom />

暂无
暂无

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

相关问题 反应警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - React Warning: Each child in a list should have a unique "key" prop 警告:列表中的每个孩子都应该有一个唯一的“key”道具,即使它有唯一的 key React - Warning: Each child in a list should have a unique “key” prop even tho it has unique key React 反应警告:列表中的每个孩子都应该有一个唯一的“钥匙”道具,当钥匙被提供并且是唯一的 - React Warning: Each child in a list should have a unique "key" prop when key is provided and unique 反应警告消除警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - React Warning eliminate 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 警告:列表中的每个孩子都应该有一个唯一的“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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM