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

Copying the graph data to itself results in orphan nodes

Open Harry-OBrien opened this issue 1 year ago • 1 comments

Describe the bug When updating graph data, if we copy the links and only update the nodes, the links no longer connect to the node. The image below changes the node with title 'targeteee' to 'target'.

Screenshot 2024-08-18 at 12 09 21 Screenshot 2024-08-18 at 12 09 29

To Reproduce

const [graphData, setGraphData] = useState({ nodes: [{id:0, title:'a'}, {id:1, title:'b'}], links: [{source: 0, target:1}]});
setGraphData({
  ...graphData,
  nodes: [{id:0, title:'c'}, {id:1, title:'b'}]
});

Expected behavior A clear and concise description of what you expected to happen.

Node changes, still connected.

Screenshot 2024-08-18 at 12 15 02 Screenshot 2024-08-18 at 12 14 57

If I 'clean' the links first, it works fine:

const updatedNodes = graphData.nodes.map(node =>
  node.id === selectedNode.id ? selectedNode : node
);

const cleanedLinks = graphData.links.map(link => {
    return { source: link.source.id, target: link.target.id };
});

setGraphData({
  links: cleanedLinks,
  nodes: updatedNodes,
});

I suspect it has to do with all of the other information that gets added to the links such as x, y, vx, vy, and information about the source and target nodes.

Screenshots If applicable, add screenshots to help explain your problem.

See above

Desktop (please complete the following information):

  • OS: [e.g. iOS] MacOs
  • Browser [e.g. chrome, safari] Firefox
  • Version [e.g. 22]

Smartphone (please complete the following information): N/A

Additional context Add any other context about the problem here. Related - not the same problem, but a similar solution.

Harry-OBrien avatar Aug 18 '24 11:08 Harry-OBrien

Not sure but think D3 kinda works like that. For every cycle you provide information. I stand to be corrected!

anyuruf avatar Sep 06 '24 14:09 anyuruf