【发布时间】:2020-03-27 07:46:12
【问题描述】:
我收到此警告,但我不知道如何解决。我认为它必须与 react-tables-2 一起使用,完整的警告是这样说的:
index.js:1375 Warning: Each child in a list should have a unique "key" prop. See..
in Cell (created by ReactTable)
in div (created by TdComponent)
in TdComponent (created by ReactTable)
in div (created by TrComponent)
in TrComponent (created by ReactTable)
in div (created by TrGroupComponent)
in TrGroupComponent (created by ReactTable)
in div (created by Tbody)
in Tbody (created by ReactTable)
in div (created by TableComponent)
in TableComponent (created by ReactTable)
in div (created by ReactTable)
in ReactTable (at CustomReactTable.js:106)
in div (at CustomReactTable.js:99)
in CustomReactTable (created by Context.Consumer)
in withRouter(CustomReactTable) (at ProductIndexComponent.js:69)
in div (at ProductIndexComponent.js:68)
in ProductIndexComponent (created by Context.Consumer)
in withRouter(ProductIndexComponent) (created by Context.Consumer)
in Route (at Wrapper.js:38)
in div (created by Sidebar)
in div (created by Sidebar)
in Sidebar (at SideBar.js:62)
in div (at SideBar.js:61)
in SideBar (created by Context.Consumer)
in withRouter(SideBar) (at Wrapper.js:36)
in div (at AuthenticatedComponent.js:33)
in AuthenticatedComponent (created by Context.Consumer)
in withRouter(AuthenticatedComponent) (at Wrapper.js:35)
in div (at Wrapper.js:34)
in Wrapper (created by Context.Consumer)
in Route (at App.js:28)
in Switch (at App.js:26)
in Router (created by BrowserRouter)
in BrowserRouter (at App.js:25)
in div (at App.js:24)
in App (at src/index.js:10)
我在控制台中两次收到此警告,我检查并尝试使用这些键但它不起作用,有谁知道我应该怎么做才能消除此警告?
编辑: 这是通用表的render方法,然后我对我需要的每个表都使用这个组件,例如ProductTable,我调用这个组件。
render() {
return (
<div className='main-content'>
{this.state.showDelete && (
<DeleteComponent onCancelDeleteClick={this.onCancelDeleteClick} item={this.state.item} />
)}
<h3>{this.props.modelName}</h3>
{this.loadAddButton()}
{this.loadFunctionalities()}
<ReactTable
data={this.state.data}
columns={this.props.columns}
manual
onFetchData={this.onFetchData}
pages={this.state.pages}
>
</ReactTable>
<div className="total-records-tag">{this.props.modelName}: {this.state.totalItems}</div>
</div >
)
} }
ProductComponent 渲染方法
render() {
return (
<div className='main-content'>
<CustomReactTable columns={this.state.columns} modelName={"Products"} />
</div >
)
}
【问题讨论】:
-
您需要为数组(列表)中呈现的每个子项提供唯一的
key。请分享您的渲染方法 -
您是否在为表格使用库?或者你只是重复 tr` 标签?如果是这样,
key属性应该在trs 元素上.. -
我正在使用 react-table 库,这就是为什么我无法控制 tr 和 td..等等
-
@nacho,你有什么解决办法吗?
-
嗨@dileepkumar jami,我做过,但现在我不记得了..我会检查一下,如果我记得我会告诉你
标签: reactjs react-table