question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Two columns have same dataField will cause the same key warning of React

See original GitHub issue

In some case, we need to add two columns which bound to same dataField, but show different test, for example, a filed of Date will be shown in two columns, one will show and edit for day part, and the other will show and edit for time part. in this case, we will get the below warning:

Warning: Encountered two children with the same key, time. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and

could change in a future version. in tr (created by Header) in thead (created by Header) in Header (created by BootstrapTable) in table (created by BootstrapTable) in div (created by BootstrapTable) in BootstrapTable (created by Context.Consumer) in SelectionProvider (created by Context.Consumer) in SortProvider (created by Context.Consumer) in CellEditProvider (created by Context.Consumer) in DataProvider (created by Table) in Table (created by Table) in Table (created by AppDefaultTable) in div (created by AppDefaultTable) in AppDefaultTable (created by Table) in Table (created by TableWithToolbar) in TableWithToolbar (created by Page) in div (created by Row) in div (created by Row) in div (created by Row) in article (created by Row) in div (created by Row) in Row (created by Page) in section (created by PageLayout) in PageLayout (created by Page) in Page (created by Connect(Page)) in Connect(Page) (created by Route) in Route (created by App) in Switch (created by App) in Router (created by BrowserRouter) in BrowserRouter (created by App) in Provider (created by App) in App

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:13
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
hkar19commented, Apr 8, 2021

in my use case, instead of relying on datafield, i just use the row value instead.

so on colum descriptions instead of:

let colums = [
.
.
.
  {datafield: 'address', text: 'City' , formatter: cityFormatter},
  {datafield: 'address', text: 'Province' , formatter: cityFormatter},
  {datafield: 'address', text: 'Country' , formatter: cityFormatter},
.
.
.
]

i do:

let colums = [
.
.
.
  {datafield: 'city', text: 'City' , formatter: cityFormatter},
  {datafield: 'province', text: 'Province' , formatter: cityFormatter},
  {datafield: 'country', text: 'Country' , formatter: cityFormatter},
.
.
.
]

in the formatter, the cell will be undefined, but you can retrieve the data by using row such as:

const cityFormatter = (cell, row)=>{
   let val = row.address;
  // rest of your code
}
3reactions
JeffreyCAcommented, Apr 28, 2020

A workaround is to declare one of the columns to be a dummy field and provide it a custom formatter:

{
      dataField: 'dummy_field',
      isDummyField: true,
      text: '',
      formatter: (cellContent, row) => {

      }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Two columns have same dataField will cause the same key warning ...
In some case, we need to add two columns which bound to same dataField, but show different test, for example, a filed of...
Read more >
unique key prop error in bootstrap-react-table2 - Stack Overflow
your "keyField" value need to use your columns's "dataField" value which is unique. for example (watch out "uniqueData" 's position):
Read more >
Columns Props · react-bootstrap-table2
If column is dummy, the column.dataField can be any string value, cause of it's meaningless. However, please keep dataField as unique as possible....
Read more >
Encountered Two Children with the Same Key — React Fix
Stable: The key for a certain element should always be the same. This is why using the array indexes can cause errors. If...
Read more >
JavaScript Data Grid Configuration - DevExtreme - DevExpress
When the columnAutoWidth property is set to false, all columns have identical width, which in turn depends on the width of the UI...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found