简体   繁体   English

我们可以在 React.js 中将假密钥传递给孩子吗?

[英]Can we pass fake keys to children in React.js?

I have created a component and it's running well in local server.我创建了一个组件,它在本地服务器上运行良好。 But I am getting below warning但我低于警告

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

Getting this warning means we need to fix the key index props?收到此警告意味着我们需要修复关键索引道具? as given here .这里给出的。 below is some snippets of my component code..下面是我的组件代码的一些片段..

    render() {
        return (
            <div>
                        <Container>                                                           
                            <Row>                                                           
                                <Col className="col-12">
                                {this.state.client.map((val,index)=>{
                                    if(index == this.state.colaborators.length -1)
                                        return <a href={"/users/"+val}>{val}</a>
                                    return <a href={"/users/"+val}>{val} ,</a>
                                })}
                                </Col>                                                              
                            </Row>
                        </Container>             
                    </div>
                </div>
            </div >
        );
    }
}

export default App;

I checked some solution from here从这里检查了一些解决方案

As I told my code is working well.正如我所说,我的代码运行良好。 Can we use some fake key props?我们可以使用一些假钥匙道具吗? for example例如

key={fake index}

And we are using will this affect in my working code?我们正在使用这会影响我的工作代码吗?

If this.state.client ever changes, don't just use the index (which is sadly common);如果this.state.client发生变化,不要只使用索引(可悲的是,这很常见); see this article for why and its demo of what can go wrong .请参阅这篇文章了解 go 错误的原因及其演示 You can only do that with a list that never changes, or only grows/shrinks (and not at the same time), not with one where the order changes (you insert at the beginning, or sort, or...) More in the docs .您只能使用永远不会更改或仅增长/缩小(而不是同时)的列表来执行此操作,而不能使用顺序更改的列表(您在开头插入,或排序,或...文档

I'm guessing val will be unique in the list, so use that as the key :我猜val在列表中是唯一的,所以用它作为key

{this.state.client.map((val, index) => {
    const href = "/users/" + val;
    const display = index == this.state.colaborators.length - 1 ? val : `${val} ,`;
    return <a key={val} href={href} >{display}</a>;
})}

If your lists order is not going to change, simply use:如果您的列表顺序不会改变,只需使用:

  return <a key={index} href={"/users/"+val}>{val}</a>
     return <a key={index} href={"/users/"+val}>{val} ,</a>

It will not affect your code and it will remove the warning.它不会影响您的代码,并且会删除警告。

You can use a npm package like UUID您可以使用 npm package 之类的UUID

import it like: import uuid from 'react-uuid' and use it like <li key={uuid()}>{item}</li>像这样导入它: import uuid from 'react-uuid'并像<li key={uuid()}>{item}</li>一样使用它

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM