react-bootstrap-table icon indicating copy to clipboard operation
react-bootstrap-table copied to clipboard

Warning: Each child in an array or iterator should have a unique "key" prop

Open mysticknits opened this issue 10 years ago • 8 comments

This might be silly but an array of JSON data throws this warning.

mysticknits avatar Jul 28 '16 22:07 mysticknits

Hi @laurenamy, This warn is not easily to find out, it happens when use map and didn't given key props, anyway if you have happened again, you may take a picture here, Thanks ~

madeinfree avatar Jul 29 '16 08:07 madeinfree

Hi @madeinfree, The warning I'm experiencing occurs since v 2.3.8. No warning in v 2.3.7. Warning: flattenChildren(...): Encountered two children with the same key, Abc. Child keys must be unique; when two children share a key, only the first child will be used. in tbody (created by TableBody) in table (created by TableBody) in div (created by TableBody) in TableBody (created by BootstrapTable) in div (created by BootstrapTable) in div (created by BootstrapTable) in BootstrapTable

microdou avatar Jul 30 '16 17:07 microdou

@microdou Thanks, I'll check it later !

madeinfree avatar Jul 30 '16 17:07 madeinfree

Sorry for lately reply. Any sample code can reproduce this issue? Thanks :)

AllenFang avatar Aug 02 '16 12:08 AllenFang

I have encountered this problem when using array of objects that returns map. The solution I made is to convert it. I'm not sure if this is a good solution, but it works in my case.

var arrList=[];
objects.map((obj) =>
           {  var a = {
                     prop1 : obj.get("prop1"),
                     prop2 : obj.get("prop2"),
                     n : obj.get("n")
                    };
               arrList.push(a);
           }
        ); 

lmalvaran avatar Aug 03 '16 00:08 lmalvaran

@laurenamy, the snippet you show up is that your solution?

AllenFang avatar Aug 03 '16 08:08 AllenFang

@AllenFang It appears that now a unique value is somehow required in each object of the data array since v2.3.8. This unique value is used to provide a unique Key in Table. When values in a dataField are not unique, error occurs. My previous data array and TableHeaderColumn:

Data = [
    {name:"apple"},
    {name:"orange"},
    {name:"orange"},
]
<BootstrapTable data={Data}>
  <TableHeaderColumn dataField="name" isKey={true}>Name</TableHeaderColumn>
</BootstrapTable>

Unique id is added to new data, and is set as key via isKey={true}. Error and warnings are now gone.

Data = [
    {id: 0, name:"apple"},
    {id: 1, name:"orange"},
    {id: 2, name:"orange"},
]
<BootstrapTable data={Data}>
  <TableHeaderColumn dataField="id" isKey={true}>Name</TableHeaderColumn>
  <TableHeaderColumn dataField="name">Name</TableHeaderColumn>
</BootstrapTable>

microdou avatar Aug 06 '16 20:08 microdou

@microdou, I got it. Actually, if isKey been applied, you should ensure the key column should not duplicated. If there's no any unique columns, I suggest you generate a set of sequence number to applied as key column.

AllenFang avatar Aug 09 '16 01:08 AllenFang