警告:列表中的每个孩子都应该有一个唯一的“关键”道具。反应表

问题描述 投票:0回答:1

我收到此警告,但我不知道如何解决。我认为它必须与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)

我在控制台中两次收到此警告,并且已经检查并尝试使用这些键,但是该键不起作用,有人知道我该怎么做才能消除此警告?

reactjs react-table
1个回答
1
投票

React通过仅渲染需要刷新的内容进行优化。循环可以倒出要渲染的非唯一组件。您可以使用按键道具解决假设您有一个雇员对象列表。假设每个员工都有一个ID

employees.map(x=> <MyComponent key={x.id}/>)
© www.soinside.com 2019 - 2024. All rights reserved.