useLayout

A React hook that provides a callback to invoke the configured layout algorithm on the given data.

const edgeTypes = {
  default: PolylineEdge
}

const nodeTypes = {
  default: MultiHandleNode
}

const LayoutFlow = () => {
  const [nodes] = useNodesState(initialNodes)
  const [edges] = useEdgesState(initialEdges)

  const { runLayout, running } = useLayout()

  return (
    <ReactFlow
      nodes={nodes}
      edges={edges}
      nodeTypes={nodeTypes}
      edgeTypes={edgeTypes}
    >
      <Panel position="top-right">
        <button onClick={() => runLayout('OrthogonalLayout')} disabled={running}>Orthogonal Layout</button>
      </Panel>
    </ReactFlow>
  )
}

export default function Flow() {
  return (
    <ReactFlowProvider>
      <LayoutFlow />
    </ReactFlowProvider>
  )
}

Parameters

NameDescriptionType
context?
The context information for running the layout algorithm.
LayoutContext

Returns

TypeDescription
{
runLayout: (
configuration:
| LayoutConfiguration
| LayoutName
) => void
running: boolean
}
Returns a function to invoke the layout algorithm and a running state that indicates whether a layout is currently calculated.