How to remove connectors from diagram if it do not gets connected to any node in React?
This article explains how to remove connectors from a diagram if they do not get connected to any node in React. In the React Diagram, the connectors are used to create a link between two points, ports, or nodes to represent the relationship between them. To connect the connector between two nodes, the connector must have a sourceID and a targetID.
You can remove the connector from the diagram when the connector’s end is not connected to the node. The connector’s sourceID and targetID properties are used to identify whether the connector is connected to a node or not.
The following code shows, how to remove the connector when the connector is not connected to the node on each end.
// Remove the connector when it is not connected to the node
function remove() {
for (let i = 0; i < diagramInstance.connectors.length; i++) {
let connector = diagramInstance.connectors[i];
if (connector.sourceID === '' || connector.targetID === '') {
diagramInstance.remove(connector);
--i;
}
}
};
//Initialize the diagram
<DiagramComponent id="diagram" ref={diagram => (diagramInstance = diagram)}
width={"100%"} height={"499px"} nodes={nodes} connectors={connectors}
getConnectorDefaults= {getConnectorDefaults} getNodeDefaults={getNodeDefaults}>
</DiagramComponent>
In the above code sample, the onClick event is used to remove the connectors, which are not connected to any nodes. The onClick event will get triggered after the Remove button is clicked. In that event, all the connectors in the diagram are iterated to check whether the sourceID and targetID are present in the connector. If the connector is not connected to any node, then the sourceID and targetID will be set as “”. If the sourceID and targetID are empty, the remove() method will remove that connector.
Refer to the working sample for additional details and implementation: Sample
Conclusion
We hope you enjoyed learning about how to remove connectors from a diagram if it does not get connected to any node in React. You can refer to our React Diagram feature tour page to know about its other groundbreaking feature representations and documentations. You can also explore our React Diagram example to understand how to present and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our React Spreadsheet and other components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!