ReactFlowInstance
The ReactFlowInstance provides a collection of methods to query and manipulate the
internal state of your flow. You can get an instance by using the
useReactFlow hook or attaching a listener to the
onInit event.
Fields
Nodes and edges
| Name | Type | Default |
|---|---|---|
getNodes | () => Node<Record<string, unknown>, string | undefined>[]Returns nodes. | |
setNodes | (payload: Node<Record<string, unknown>, string | undefined>[] | ((nodes: Node<Record<string, unknown>, string | undefined>[]) => Node<Record<string, unknown>, string | undefined>[])) => voidSet your nodes array to something else by either overwriting it with a new array or by passing
in a function to update the existing array. If using a function, it is important to make sure a
new array is returned instead of mutating the existing array. Calling this function will
trigger the | |
addNodes | (payload: Node<Record<string, unknown>, string | undefined> | Node<Record<string, unknown>, string | undefined>[]) => voidAdd one or many nodes to your existing nodes array. Calling this function will trigger the
| |
getNode | (id: string) => Node<Record<string, unknown>, string | undefined> | undefinedReturns a node by id. | |
getInternalNode | (id: string) => InternalNode<Node<Record<string, unknown>, string | undefined>> | undefinedReturns an internal node by id. | |
getEdges | () => Edge<Record<string, unknown>, string | undefined>[]Returns edges. | |
setEdges | (payload: Edge<Record<string, unknown>, string | undefined>[] | ((edges: Edge<Record<string, unknown>, string | undefined>[]) => Edge<Record<string, unknown>, string | undefined>[])) => voidSet your edges array to something else by either overwriting it with a new array or by passing
in a function to update the existing array. If using a function, it is important to make sure a
new array is returned instead of mutating the existing array. Calling this function will
trigger the | |
addEdges | (payload: Edge<Record<string, unknown>, string | undefined> | Edge<Record<string, unknown>, string | undefined>[]) => voidAdd one or many edges to your existing edges array. Calling this function will trigger the
| |
getEdge | (id: string) => Edge<Record<string, unknown>, string | undefined> | undefinedReturns an edge by id. | |
toObject | () => ReactFlowJsonObject<Node<Record<string, unknown>, string | undefined>, Edge<Record<string, unknown>, string | undefined>>Returns the nodes, edges and the viewport as a JSON object. | |
deleteElements | (params: DeleteElementsOptions) => Promise<{ deletedNodes: Node<Record<string, unknown>, string | undefined>[]; deletedEdges: Edge<Record<string, unknown>, string | undefined>[]; }>Deletes nodes and edges. | |
updateNode | (id: string, nodeUpdate: Partial<Node<Record<string, unknown>, string | undefined>> | ((node: Node<Record<string, unknown>, string | undefined>) => Partial<Node<...>>), options?: { ...; } | undefined) => voidUpdates a node. | |
updateNodeData | (id: string, dataUpdate: Partial<Record<string, unknown>> | ((node: Node<Record<string, unknown>, string | undefined>) => Partial<Record<string, unknown>>), options?: { ...; } | undefined) => voidUpdates the data attribute of a node. | |
changeParent | (nodeId: string, parentId: string | null) => voidChanges the parent of a node. | |
updateEdge | (id: string, edgeUpdate: Partial<Edge<Record<string, unknown>, string | undefined>> | ((edge: Edge<Record<string, unknown>, string | undefined>) => Partial<Edge<...>>), options?: { ...; } | undefined) => voidUpdates an edge. | |
updateEdgeData | (id: string, dataUpdate: Partial<Record<string, unknown> | undefined> | ((edge: Edge<Record<string, unknown>, string | undefined>) => Partial<Record<string, unknown> | undefined>), options?: { ...; } | undefined) => voidUpdates the data attribute of a edge. | |
getNodesBounds | (nodes: (string | Node<Record<string, unknown>, string | undefined> | InternalNode<Node<Record<string, unknown>, string | undefined>>)[]) => RectReturns the bounds of the given nodes or node ids. | |
getNodeConnections | ({ type, handleId, nodeId }: { type?: HandleType; nodeId: string; handleId?: string | null; }) => NodeConnection[]Gets all connections to a node. Can be filtered by handle type and id. |
Intersections
| Name | Type | Default |
|---|---|---|
getIntersectingNodes | (node: Node<Record<string, unknown>, string | undefined> | Rect | { id: string; }, partially?: boolean | undefined, nodes?: Node<Record<string, unknown>, string | undefined>[] | undefined) => Node<...>[]Find all the nodes currently intersecting with a given node or rectangle. The | |
isNodeIntersecting | (node: Node<Record<string, unknown>, string | undefined> | Rect | { id: string; }, area: Rect, partially?: boolean | undefined) => booleanDetermine if a given node or rectangle is intersecting with another rectangle. The |
Viewport
| Name | Type | Default |
|---|---|---|
zoomIn | (options?: { duration?: number; ease?: (t: number) => number; interpolate?: "smooth" | "linear"; }) => Promise<boolean>Zooms viewport in by 1.2. | |
zoomOut | (options?: { duration?: number; ease?: (t: number) => number; interpolate?: "smooth" | "linear"; }) => Promise<boolean>Zooms viewport out by 1 / 1.2. | |
zoomTo | (zoomLevel: number, options?: { duration?: number; ease?: (t: number) => number; interpolate?: "smooth" | "linear"; }) => Promise<boolean>Zoom the viewport to a given zoom level. Passing in a | |
getZoom | () => numberGet the current zoom level of the viewport. | |
setViewport | (viewport: Viewport, options?: { duration?: number; ease?: (t: number) => number; interpolate?: "smooth" | "linear"; }) => Promise<boolean>Sets the current viewport. | |
getViewport | () => ViewportReturns the current viewport. | |
setCenter | (x: number, y: number, options?: ViewportHelperFunctionOptions & { zoom?: number; }) => Promise<boolean>Center the viewport on a given position. Passing in a | |
fitBounds | (bounds: Rect, options?: ViewportHelperFunctionOptions & { padding?: Padding; }) => Promise<boolean>A low-level utility function to fit the viewport to a given rectangle. By passing in a
| |
screenToFlowPosition | (clientPosition: XYPosition, options?: { snapToGrid?: boolean; snapGrid?: SnapGrid; } | undefined) => XYPositionWith this function you can translate a screen pixel position to a flow position. It is useful for implementing drag and drop from a sidebar for example. | |
flowToScreenPosition | (flowPosition: XYPosition) => XYPositionTranslate a position inside the flow’s canvas to a screen pixel position. | |
viewportInitialized | booleanReact Flow needs to mount the viewport to the DOM and initialize its zoom and pan behavior. This property tells you when viewport is initialized. | |
fitView | (fitViewOptions?: { padding?: Padding; includeHiddenNodes?: boolean; minZoom?: number; maxZoom?: number; duration?: number; ease?: (t: number) => number; interpolate?: "smooth" | "linear"; nodes?: (NodeType | { id: string; })[]; }) => Promise<boolean>Fits the view based on the passed params. By default it fits the view to all nodes. |