Skip to content

Use Nodes Data

Nodes that compute from the data of the nodes connected to them, as React Flow's useNodesData and useNodeConnections do. The source holds a number in its Data; every other node works its value out from its incoming connections as it renders, so changing the number or rewiring an edge updates everything downstream.

Source 25
Double inputs × 250
Square inputs squared625
Sum inputs added675
bit Blow

Try it

  • Drag the slider in Source: Double, Square and Sum all recompute as it moves.
  • Click the edge from Square to Sum and press Delete: Sum now adds up only what still reaches it. Delete the edge into Double and Double shows no input.
  • Drag from a handle to rewire: connect Source straight to Sum, or Double into Square, and the values follow the new wiring.
  • Drag a node anywhere: where it sits changes nothing, only what is connected to it.

How it works

  • A node's Data holds whatever the application needs: a Counter on the source, and an Operation on each computing node. The NodeTemplate draws each by the type of its data.
  • A computing node's value is worked out as the template renders it: BlowGraphUtils.GetIncomers(node, nodes, edges) returns the nodes with an edge into it - what useNodeConnections answers - and their data gives its inputs - what useNodesData reads. The operation is applied to the sum of the inputs; with none, there is no value.
  • Nothing computed is stored on a node, so nothing can go stale: the value is always that of the graph as it is drawn. A walk that comes back to a node it is already computing stops there, so wiring a loop shows no value rather than hanging.
  • The slider carries the nodrag class, the canvas's default NoDragClass, and stops its own pointer-down, so dragging it moves the value and not the node.
  • Moving the slider runs a handler of the page, which re-renders the page and with it the canvas and every template. EdgesChanged does the same when an edge is connected or deleted on the canvas.
An unhandled error has occurred. Reload 🗙