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

Updating state causes id already exists error #19

Open
silvercondor opened this issue May 12, 2020 · 6 comments
Open

Updating state causes id already exists error #19

silvercondor opened this issue May 12, 2020 · 6 comments

Comments

@silvercondor
Copy link

silvercondor commented May 12, 2020

Am I updating the state right?

this.state = {
    exampleGraph:{
        nodes: [],
        edges: []
    }
fetchState(){
    //api call with list of values
    this.setState({exampleGraph:{nodes: api_res.nodes, edges: api_res.edges})
}

Not the exact code but I'm doing something like this.

which gives me the following error

Error: Cannot add item: item with id 1 already exists

Stacktrace shows:

this.setState(
{ 
 exampleGraph: {nodes:node_list, edges: api_res_edges},
}
@anishmprasad
Copy link
Owner

please share api_res object

@oculardexterity
Copy link

oculardexterity commented Aug 21, 2020

I have same issue, I think.

Graph is in react state:

const [graph, setGraph] = useState({
    nodes: [
      { id: 1, label: "Node 1" },
      { id: 2, label: "Node 2" },
      { id: 3, label: "Node 3" },
      { id: 4, label: "Node 4" },
      { id: 5, label: "Node 5" },
    ],
    edges: [
      { from: 1, to: 2},
      { from: 1, to: 3 },
      { from: 2, to: 4 },
      { from: 2, to: 5 },
    ],
  });

But when I call:

   setGraph({
      nodes: [...graph.nodes, { id: "6", label: 'Node 6' }],
      edges: [...graph.edges],
    });

I get the error Uncaught Error: Cannot add item: item with id 6 already exists

I guess this is the same error as #8: the add node function is being called twice for some reason, thus producing error

@danwt
Copy link

danwt commented Sep 29, 2020

Same issue here

@leonardorodd
Copy link

Same

@j6k4m8
Copy link

j6k4m8 commented Dec 13, 2020

Hitting the same problem here. For some more diagnostic color, I randomly generated a string in render() and prepend all node IDs with that string; this is calling twice in the same render cycle (i.e. the issue is not calling render twice etc).

@flatplate
Copy link

I have the same problem. I have found that supplying a new key every time you update the data works. I update the graph like this:

let [graph, setGraph] = useState({ nodes: [], edges: [], rand: "" });
...
setGraph({
    nodes: nodes,
    edges: edges,
    rand: Math.random().toString(),
});

And I use the Graph element like this:

<Graph
    key={graph.rand}
    ...
/>

Hope this helps until we can find a better solution.

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