简体   繁体   English

React - 列表中的每个孩子都应该有一个唯一的“key”道具

[英]React - Each child in a list should have a unique “key” prop

In My react component, My UI loads perfectly but there is console error which i wanted to fix error says - ' Each child in a list should have a unique "key" prop ' for below code.在我的反应组件中,我的 UI 加载完美,但有控制台错误,我想修复错误说 - ' Each child in a list should have a unique "key" prop ',用于下面的代码。 Guide me how I can pass keys to detailsContent指导我如何将密钥传递给detailsContent

render() {
    const header = (
        <div className={css.deviceDetailsBody}>
            <div className="sidebar__header">
                <div className="sidebar__title">
                    <div>
                        {hostname}
                        {this._renderCloser()}
                        <div className={"sidebar__subtitle"}>
                            <div className="toolsBar">
                                <div className="toolsBarlet">
                                    <div className="toolsItemNarrow">{deviceTypeView}</div>
                                    {complianceStatusView}
                                    <div className="toolsItemNarrow">{managementIpAddress}</div>
                                    {deviceUpTime}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    );
    const horizontalLine = <div className="sidebar__horiz_line" />;
    const body = (
        <div className="body">
            {tabs}
            {tools}
        </div>
    );

    const detailsContent = [header, horizontalLine, body].map(
        currentComponent => currentComponent
    );

    return (
        <div>
            {sideBarShow && (
                <Sidebar {...sidebarProps}>
                    <div>{detailsContent}</div>
                </Sidebar>
            )}
        </div>
    );
}

Added the header, body code for more clarity.添加了 header,正文代码更清晰。

Each child in a list should have a unique "key" prop列表中的每个孩子都应该有一个唯一的“关键”道具

If using Components and not plain jsx elements, simply add a key prop to the currentComponent jsx.如果使用组件而不是普通的 jsx 元素,只需将key prop 添加到currentComponent jsx。 Note for simplicity I just used index as key.请注意,为简单起见,我只是使用index作为键。

const detailsContent = [header, horizontalLine, body].map(
  (CurrentComponent, index) => (<CurrentComponent key={index} />)
);

See CodeSandbox见 CodeSandbox

Using elements, please refer to the accepted answer .使用元素,请参考接受的答案

You can use React.cloneElement to add keys您可以使用React.cloneElement添加键

const detailsContent = [header, line, body].map(
  (element, index) => React.cloneElement(element, {key: index})
);

暂无
暂无

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

相关问题 “反应”列表中的每个孩子都应该有一个唯一的“关键”道具 - "react" Each child in a list should have a unique "key" prop React - 列表中的每个孩子都应该有一个唯一的“key”道具 - React - Each child in a list should have a unique “key” prop 列表中的每个孩子都应该有一个唯一的“关键”道具/反应 - Each child in a list should have a unique "key" prop / react 反应错误:列表中的每个孩子都应该有一个唯一的“关键”道具 - React Error: Each child in a list should have a unique "key" prop 反应警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - React Warning: Each child in a list should have a unique "key" prop React,列表中的每个孩子都应该有一个唯一的 key prop - React, 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 NOT caused by lack of key in map 列表中的每个孩子都应该有一个唯一的“关键”道具错误,以 uuid 作为关键反应 - Each child in a list should have a unique "key" prop error with uuid as key react React - 数组或迭代器中的每个子节点都应该有一个唯一的“key”prop - React - Each child in an array or iterator should have a unique “key” prop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM