question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Node drag causes React-Flow's internal state to get out of sync

See original GitHub issue

I encountered a problem where after node is dragged, its position becomes stale and cannot be updated via changing node.position property; the root cause of this seems to be related to the following code in the reducer:

https://github.com/wbkd/react-flow/blob/4b043bceccf41ae1edbd52e2919e1bf5a65aef99/src/store/reducer.ts#L44-L46

In my testing, the condition is never true and therefore allows node.__rf.position to get out of sync with node.position; I believe the correct logic should be

if (storeNode.__rf.position.x !== propElement.position.x || storeNode.__rf.position.y !== propElement.position.y) { 
   updatedNode.__rf.position = propElement.position; 
 } 

Please note, this bug only surfaces after node position has been updated by node drag and subsequently updating the node’s position via node.position will cease to have an effect on the node’s position (ie node.__rf.position becomes out of sync with node.position).

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
datoslabscommented, Sep 23, 2021

Thank you @Danybs; the PR I submitted to fix this issue had been approved, hopefully, it will be released soon. Per my experiment and writeup above, after node drag node.position !== reflect node.__rf.position; this is why the fix needs to be made to the reducer as any attempts to programmatically update a node’s position after node drag event will not physically move the node since rendering is based on node.__rf.position NOT node.position.

2reactions
Danybscommented, Sep 23, 2021

when drag elements to flow if move nodes, dont update position state i updated elements state position this way:

    const onNodeDragStop = (event, node) => {
        let elementsCopy = elements;
        let index = elements.findIndex(element => element.id === node.id);
        let newPositionNode = elements[index];
        newPositionNode.position = node.position;
        elementsCopy.splice(index, 1, newPositionNode);
        setElements(elementsCopy);
    };

regards

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrate to v10 Guide - React Flow
It was always a bit messy to sync the local user state with the internal state ... with the ability to drag nodes,...
Read more >
Lifting State Up - React
Often, several components need to reflect the same changing data. We recommend lifting the shared state up to their closest common ancestor.
Read more >
Designing a Dataflow Editor With TypeScript and React
So we set out to make a dataflow editor with three primary properties: ... Adjacent edges feel like part of the node state,...
Read more >
copy the node while drag and drop using react-flow-renderer
You have to add the code that will update the position in the state. By default this position is only used when creating...
Read more >
A react library for building node-based graphs - Morioh
Feel free to check out the examples or read the blog post to get started. ... other elements inside your custom node that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found