Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot add item: item with id <ID> already exists. Graph not updating. #83

Open
chandrarishabh opened this issue Apr 16, 2020 · 8 comments

Comments

@chandrarishabh
Copy link

const [graphData, setGraphData] = useState(g1)

// addNode onClick function for button Change Graph.

const addNode = (e) => {
    e.preventDefault()
    console.log('addNode clicked!')
    setGraphData(g2)
  }
return
<div className='graphContainer'>
          <Graph
                graph={graphData}
                options={options}
            />
</div>

graphData is updated with correct data but component gives error saying Cannot add item with existing ID but g1 and g2 has nodes with different IDs.

@holson1
Copy link

holson1 commented Apr 17, 2020

I have the same issue - not sure how to update the graph with new data, all the examples I've seen are only with static data.

@greysonevins
Copy link

greysonevins commented Apr 17, 2020

@chandrarishabh so I had an example where I select a graph number to load with useState and got the same result of where it breaks from already exists so I added a key prop to the Graph like I would for mapping children and for some reason that resolves it for me.

basically, react needs a way to identify it as a new child of the whole component so that it doesn't rerender an older one.

<Graph
                key={selectGroup}
                graph={
                  {
                    nodes: graph.nodes.filter(n => n.network === parseInt(selectGroup)),
                    edges: graph.edges.filter(n => n.network === parseInt(selectGroup)),
                  }
                }
                options={options}
                events={events}

            />```

@chandrarishabh
Copy link
Author

@greysonevins Thanks for your input. Can you please explain me what value does selectGroup exactly holds. Do you have any working repository which I can use as reference? I just want to add nodes and edges to my graph and then from the given graph present a new graph. Any help will be much appreciated. Thanks.

@greysonevins
Copy link

@chandrarishabh I made a codesandbox for it

https://codesandbox.io/s/interesting-spence-onwor?file=/src/App.js

Basically the selectGroup in my case was an id to graph, in this example, I add a new random node and edge to the graph and use a uuid as key.

You'll see that there are two factors that fix this bug, a deep clone of the graph when reseting or adding a node and the uuid for the key.

I think the issue is that when you manipulate the data as is without deepcloning it, the graph representation of the object tells the Graph that it hasn't changed and without a key, the wrapper doesn't know it is a different graph

anindyy added a commit to anindyy/avatar-citizen-graph that referenced this issue Jun 24, 2020
Reference: crubier/react-graph-vis#83
Still not working, apparently you can't concat a deepcloned array.

Committing to save progress because I want to change the graph library
as I was stuck with vis and I'm running out of time.
@bjm88
Copy link

bjm88 commented Nov 23, 2020

this error is still happening for me, not sure if its related to using useEffect to load from an api....

@technicallyty
Copy link

@chandrarishabh I made a codesandbox for it

https://codesandbox.io/s/interesting-spence-onwor?file=/src/App.js

Basically the selectGroup in my case was an id to graph, in this example, I add a new random node and edge to the graph and use a uuid as key.

You'll see that there are two factors that fix this bug, a deep clone of the graph when reseting or adding a node and the uuid for the key.

I think the issue is that when you manipulate the data as is without deepcloning it, the graph representation of the object tells the Graph that it hasn't changed and without a key, the wrapper doesn't know it is a different graph

I tried this exact code in React 17. It no longer works. Error shows same as OP - node with ID 6 already exists. Downgraded to the version in the sandbox and works again. Seems there was a breaking change causing the method to now fail

@crubier
Copy link
Owner

crubier commented Feb 15, 2021

Can someone try with the latest version (1.0.7) and send a reproduction ?

@crubier crubier closed this as completed Feb 15, 2021
@crubier crubier reopened this Feb 15, 2021
@Igor2122
Copy link

hey, just wanted to share:
I am getting this error on undefined value, spent like 1,5 hours 😅 to figure out what was going on

setGraphData(({nodes, edges}) => ({
        nodes: [
          ...nodes,
          {
            id: modifiedId,
            label,
            title,
            image: image, // 
            level: 2,
          },
        ],
        edges: [...edges, {from: id, to: modifiedId}],
      }))```
      
      
`image` was `undefined` 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants