react-graph-vis icon indicating copy to clipboard operation
react-graph-vis copied to clipboard

A duplicate id was found in the parameter array error with hello world example

Open jrraymond opened this issue 4 years ago • 12 comments

Thank you for this project!

I tried to recreate the example from the readme with some very minor modifications (no explicit call to ReactDOM). The example renders correctly but fails when the component re-renders with the following error:

Error: A duplicate id was found in the parameter array.
r.value
src/data-set.ts:263
  260 | if (Array.isArray(data)) {
  261 |   // Array
  262 |   const idsToAdd: Id[] = data.map((d) => d[this._idProp] as Id);
> 263 |   if (idsToAdd.some((id) => this._data.has(id))) {
      | ^  264 |     throw new Error("A duplicate id was found in the parameter array.");
  265 |   }
  266 |   for (let i = 0, len = data.length; i < len; i++) {
import Graph from "react-graph-vis";


function App() {
  const graph = {
    nodes: [
      { id: 1, label: "Node 1", title: "node 1 tootip text" },
      { id: 2, label: "Node 2", title: "node 2 tootip text" },
      { id: 3, label: "Node 3", title: "node 3 tootip text" },
      { id: 4, label: "Node 4", title: "node 4 tootip text" },
      { id: 5, label: "Node 5", title: "node 5 tootip text" }
    ],
    edges: [
      { from: 1, to: 2 },
      { from: 1, to: 3 },
      { from: 2, to: 4 },
      { from: 2, to: 5 }
    ]
  };

  const options = {
    layout: {
      hierarchical: true
    },
    edges: {
      color: "#000000"
    },
    height: "600px"
  };

  const events = {
    select: function(event: any) {
      var { nodes, edges } = event;
    }
  };
  return (
    <Graph
      graph={graph}
      options={options}
      events={events}
    />
  );
}

export default App;

I'm using the following versions:

    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-graph-vis": "^1.0.7",
    "typescript": "^4.2.2",

jrraymond avatar Mar 03 '21 22:03 jrraymond

Check out this issue #92. In my case, It works. I hope this will help you.

hjlee94 avatar Mar 08 '21 04:03 hjlee94

@jrraymond were you able to resolve this issue?

oshhh avatar Mar 11 '21 10:03 oshhh

It happens to me every time on Reacts DOM update sequence. @crubier Can this get some attention?

vishnu-dev avatar Mar 29 '21 05:03 vishnu-dev

I think it's problem is caused by unnecessary re-rendering, which creates a new component with the same key as the previous one. This problem can be solved by differentiating components like <Graph key={Math.random()} /> or memoizing components by wrap them like React.memo(<Graph />).

The Math.random() method will play the animation every time it is re-rendered. The React.memo() method also re-renders the nodes when you add props to the Graph component itself, so the animation plays. I don't like this behavior very much. umm...

ivgtr avatar Apr 09 '21 08:04 ivgtr

I put what I've seen about where this problem is coming from on https://github.com/crubier/react-graph-vis/issues/92#issuecomment-818279270 Having to set keys to force re-creating the component isn't an ideal solution, and it'd be good for this to be StrictMode compatible.

ZachHaber avatar Apr 12 '21 22:04 ZachHaber

I think it's problem is caused by unnecessary re-rendering, which creates a new component with the same key as the previous one. This problem can be solved by differentiating components like or memoizing components by wrap them like React.memo().

The Math.random() method will play the animation every time it is re-rendered. The React.memo() method also re-renders the nodes when you add props to the Graph component itself, so the animation plays. I don't like this behavior very much. umm...

This solution work for me, thanks @ivgtr

coder-Rit avatar Oct 12 '23 15:10 coder-Rit

I also encountered this problem in React 18, and I had been solved by remove <React.StrictMode></React.StrictMode> in index.js.

YXR9 avatar Nov 24 '23 03:11 YXR9

Late to the party, but just do this instead:

const [graph, setGraph] = useState({
        nodes: [
            { id: 1, label: 'Node 1', title: 'node 1 tootip text' },
            { id: 2, label: 'Node 2', title: 'node 2 tootip text' },
            { id: 3, label: 'Node 3', title: 'node 3 tootip text' },
            { id: 4, label: 'Node 4', title: 'node 4 tootip text' },
            { id: 5, label: 'Node 5', title: 'node 5 tootip text' },
        ],
        edges: [
            { from: 1, to: 2 },
            { from: 1, to: 3 },
            { from: 2, to: 4 },
            { from: 2, to: 5 },
        ],
    });
    ```

Davste93 avatar Feb 06 '24 08:02 Davste93

I also encountered this problem in React 18, and I had been solved by remove <React.StrictMode></React.StrictMode> in index.js.

Worked for me :)