<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Paweł Piotr Owczarek</title>
    <description>The latest articles on DEV Community by Paweł Piotr Owczarek (@pshepherd).</description>
    <link>https://dev.to/pshepherd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1147670%2F00994816-a1b3-4af3-a588-2950c89af612.png</url>
      <title>DEV Community: Paweł Piotr Owczarek</title>
      <link>https://dev.to/pshepherd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pshepherd"/>
    <language>en</language>
    <item>
      <title>React-flow, edges changing place on creating new node</title>
      <dc:creator>Paweł Piotr Owczarek</dc:creator>
      <pubDate>Sun, 27 Aug 2023 20:13:02 +0000</pubDate>
      <link>https://dev.to/pshepherd/react-flow-edges-changing-place-on-creating-new-node-cg4</link>
      <guid>https://dev.to/pshepherd/react-flow-edges-changing-place-on-creating-new-node-cg4</guid>
      <description>&lt;p&gt;Edges change their place, when i create a new node.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gif example Gif showing the problem&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://gifyu.com/image/SgWWG"&gt;https://gifyu.com/image/SgWWG&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Possible cause that I can't fix&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;ReactFlowKey is updating the website without previously saving edges as they were created, because&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;"Couldn't create edge for source/target handle id: "some-id"; edge id: "some-id"."&lt;/p&gt;

&lt;p&gt;which i guess is because I cannot set the proper value to targetHandle or sourceHandle when connecting the edges with the handles.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;BiDirectionalNode.tsx *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I created custom node in BiDirectionalNode.tsx that has 4 handles (1 on each side of the square) that are sources and targets at the same time, they have unique ids&lt;/p&gt;

&lt;p&gt;Example :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Handle type="source" position={Position.Left} id="leftA" /&amp;gt;&lt;br&gt;
      &amp;lt;Handle type="target" position={Position.Left} id="leftB" /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;App.js&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I then in my App.js use this custom node, by creating it (handleClick method), and onConnect method to connect the edges and save them to this.state.initialEdges&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creates new node&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;handleClick() {&lt;br&gt;
    const newNode = {&lt;br&gt;
      id:&lt;/code&gt;${this.state.initialNodes.length + 1}`,&lt;br&gt;
      data: { label: this.state.inputText },&lt;br&gt;
      type: "bidirectional",&lt;br&gt;
      position: {&lt;br&gt;
        x: 100,&lt;br&gt;
        y: 100&lt;br&gt;
      }&lt;br&gt;
    };&lt;/p&gt;

&lt;p&gt;this.setState(&lt;br&gt;
      (prevState) =&amp;gt; ({&lt;br&gt;
        initialNodes: [...prevState.initialNodes, newNode],&lt;br&gt;
        initialEdges: updatedEdges,&lt;br&gt;
        inputText: "",&lt;br&gt;
        reactFlowKey: prevState.reactFlowKey + 1&lt;br&gt;
      }),`&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creates Edges between the nodes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;` const onConnect = (params) =&amp;gt; {&lt;br&gt;
      const { source, target, sourceHandleId, targetHandleId } = params;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  console.log("source:", source);
  console.log("target:", target);

  const newEdge1 = {
    id: `edge-${this.state.edgeCounter}`,
    source: source,
    target: target,
    type: "bidirectional",
    sourceHandle: sourceHandleId,
    targetHandle: targetHandleId
  };

  const newEdge2 = {
    id: `edge-${this.state.edgeCounter + 1}`,
    source: source,
    target: target,
    type: "bidirectional",
    sourceHandle: sourceHandleId,
    targetHandle: targetHandleId
  };

  this.setState((prevState) =&amp;gt; ({
    initialEdges: addEdge(
      newEdge1,
      addEdge(newEdge2, prevState.initialEdges)
    ),
    edgeCounter: prevState.edgeCounter + 2
  }));
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;console.log(newEdge1) after trying to connect the handles of the nodes&lt;/p&gt;

&lt;p&gt;id: "edge-0"&lt;br&gt;
source: "2"&lt;br&gt;
target: "1"&lt;br&gt;
type: "bidirectional"&lt;br&gt;
sourceHandle: undefined&lt;br&gt;
targetHandle: undefined`&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://codesandbox.io/s/affectionate-liskov-dsfzcd?file=/src/App.js"&gt;https://codesandbox.io/s/affectionate-liskov-dsfzcd?file=/src/App.js&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's the link to the full code&lt;/p&gt;

&lt;p&gt;I tried targeting the handle's unique id's, with getelement by id, targetHandleId, and I can't even remember how many more ways, I've asked copilot and gpt for help in many ways but they can't help.&lt;/p&gt;

&lt;p&gt;The main problem I guess is that I created this whole code with state method (because thats the only way I know, I am learning react for a week now), while whole docs on their react-flow website is done in a different way (hooks?) without using this.state etc.&lt;/p&gt;

&lt;p&gt;Also, I tried removing ReactFlowKey and replacing updating the website with something else, but nothing helped, and without ReactFlowKey, the nodes don't show up in html (they do in the console tho).&lt;/p&gt;

</description>
      <category>react</category>
      <category>beginners</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
