bencripps / react-redux-grid

A React Grid/Tree Component written in the Redux Pattern
http://react-redux-grid.herokuapp.com/
MIT License
444 stars 63 forks source link

Keys not unique when grabbing _id's from MongoDB #182

Open pthieu opened 7 years ago

pthieu commented 7 years ago

I'm getting a unique key conflict, I know this is a react error, but I can seem to debug it. I make sure that all the ID's are unique in each array of children, but this still pops up:

proxyConsole.js:56 Warning: flattenChildren(...): Encountered two children with the same key, `1:$tree-item-59a7227a1d41c83a55289af5`. Child keys must be unique; when two children share a key, only the first child will be used.
    in tbody (created by TableRow)
    in TableRow (created by DragDropContext(TableRow))
    in DragDropContext(TableRow) (created by TableContainer)
    in table (created by TableContainer)
    in div (created by TableContainer)
    in TableContainer (created by Grid)
    in div (created by Grid)
    in Grid (created by Connect(Grid))
    in Connect(Grid) (at ontology-tree.js:55)

My tree looks somewhat like a normal nested tree seen in most of this repo:

[{
  id
  parentId
  name
  children: [{
    id:
    parentId
    name
    children: [{...}, {...}, ...]
  }]
}]

Sometimes the a nested array of children may have some of the same elements as other arrays of children, but every individual array of children will not have duplicated id. Been trying to debug this for a while, is there a way to quickly see what the two conflicts are? I check Redux-Devtools for the conflict but they're not in the state so I can't see which elements are in conflict.

pthieu commented 7 years ago

Upon some reading: https://github.com/bencripps/react-redux-grid/blob/master/docs/CREATE_KEY_FROM.md

It seems that even if your child is at another parent, if they have they same _key, it's going to conflict.

What's the correct structure to have the same child in multiple parents: i.e.:

{
  adult1: {
    children: [{
      id: 'child1'
    }]
  }
  adult2: 
    children: [{
      id: 'child1'
    }]
}

Kind of how a child shoes can show up under a parent sports or luxury

bencripps commented 7 years ago

In this scenario, since your keys are not unique you shouldn't be specifying the createKeyFrom property. Grid will create unique keys by default (it just won't be as performant).

pthieu commented 7 years ago

@bencripps: so would i pass in createKeyFrom: false as a parameter to disable?

bencripps commented 7 years ago

That should work, but you could also not pass in the property at all. I believe it defaults to false.

pthieu commented 7 years ago

Originally I did not pass it in at all and was still getting this error. Let me try it with it explicitly set to false.

bencripps commented 6 years ago

Any update on this?