Hello! I have an app using React Flow with some state that includes nodes & edges, like this:
const [nodes, setNodes] = useNodesState<NodeData>([]);
const [edges, setEdges] = useEdgesState<EdgeData>([]);
I then have both onConnect and onConnectEnd callbacks defined. In both callbacks, I want to edit the edges, something like this:
const onConnect = useCallback((connection: Connection) => {
// onConnect can add a new edge
const newEdges = deepCopy(edges);
newEdges.push({/* new edge data */});
setEdges(newEdges);
}, [edges]);
const onConnectEnd = useCallback(() => {
// onConnectEnd unchecks some flags on existing edges
const newEdges = deepCopy(edges);
for (const edge of newEdges) {
edge.flag = false;
}
setEdges(newEdges);
}, [edges]);
HOWEVER, I noticed that onConnectEnd will never see the edges set by onConnect, so it will basically "undo" the added edge because it will reset the edges to the previous stale state. I assume this is because both callbacks are invoked by the same caller, so they are bound to the same state "snapshot" of state - onConnectEnd doesn't get a chance to be updated and pick up the new state before it is called. Is there anything I can do about this?