Skip to Content
React Flow 13 is here! Read the announcement →
ReferenceTypes

ReactFlowInstance

Source on GitHub 

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

NameTypeDefault
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>[])) => void

Set 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 onNodesChange handler in a controlled flow.

addNodes(payload: Node<Record<string, unknown>, string | undefined> | Node<Record<string, unknown>, string | undefined>[]) => void

Add one or many nodes to your existing nodes array. Calling this function will trigger the onNodesChange handler in a controlled flow.

getNode(id: string) => Node<Record<string, unknown>, string | undefined> | undefined

Returns a node by id.

getInternalNode(id: string) => InternalNode<Node<Record<string, unknown>, string | undefined>> | undefined

Returns 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>[])) => void

Set 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 onEdgesChange handler in a controlled flow.

addEdges(payload: Edge<Record<string, unknown>, string | undefined> | Edge<Record<string, unknown>, string | undefined>[]) => void

Add one or many edges to your existing edges array. Calling this function will trigger the onEdgesChange handler in a controlled flow.

getEdge(id: string) => Edge<Record<string, unknown>, string | undefined> | undefined

Returns 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) => void

Updates a node.

updateNodeData(id: string, dataUpdate: Partial<Record<string, unknown>> | ((node: Node<Record<string, unknown>, string | undefined>) => Partial<Record<string, unknown>>), options?: { ...; } | undefined) => void

Updates the data attribute of a node.

changeParent(nodeId: string, parentId: string | null) => void

Changes 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) => void

Updates 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) => void

Updates the data attribute of a edge.

getNodesBounds(nodes: (string | Node<Record<string, unknown>, string | undefined> | InternalNode<Node<Record<string, unknown>, string | undefined>>)[]) => Rect

Returns 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

NameTypeDefault
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 partially parameter can be set to true to include nodes that are only partially intersecting.

isNodeIntersecting(node: Node<Record<string, unknown>, string | undefined> | Rect | { id: string; }, area: Rect, partially?: boolean | undefined) => boolean

Determine if a given node or rectangle is intersecting with another rectangle. The partially parameter can be set to true return true even if the node is only partially intersecting.

Viewport

NameTypeDefault
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 duration will animate the viewport to the new zoom level.

getZoom() => number

Get 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() => Viewport

Returns the current viewport.

setCenter(x: number, y: number, options?: ViewportHelperFunctionOptions & { zoom?: number; }) => Promise<boolean>

Center the viewport on a given position. Passing in a duration will animate the viewport to the new position.

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 duration, the viewport will animate from its current position to the new position. The padding option can be used to add space around the bounds.

screenToFlowPosition(clientPosition: XYPosition, options?: { snapToGrid?: boolean; snapGrid?: SnapGrid; } | undefined) => XYPosition

With 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) => XYPosition

Translate a position inside the flow’s canvas to a screen pixel position.

viewportInitializedboolean

React 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.