useReactFlow()
This hook returns a ReactFlowInstance
that can
be used to update nodes and edges, manipulate the viewport, or query the current
state of the flow.
import { useCallback, useState } from 'react';
import { useReactFlow } from '@xyflow/react';
export function NodeCounter() {
const reactFlow = useReactFlow();
const [count, setCount] = useState(0);
const countNodes = useCallback(() => {
setCount(reactFlow.getNodes().length);
// you need to pass it as a dependency if you are using it with useEffect or useCallback
// because at the first render, it's not initialized yet and some functions might not work.
}, [reactFlow]);
return (
<div>
<button onClick={countNodes}>Update count</button>
<p>There are {count} nodes in the flow.</p>
</div>
);
}
Signature
Parameters:This function does not accept any parameters.
Returns:TypeScript
This hook accepts a generic type argument of custom node & edge types. See this section in our TypeScript guide for more information.
const reactFlow = useReactFlow<CustomNodeType, CustomEdgeType>();
Notes
- This hook can only be used in a component that is a child of a
<ReactFlowProvider />
or a<ReactFlow />
component. - Unlike
useNodes
oruseEdges
, this hook won’t cause your component to re-render when state changes. Instead, you can query the state when you need it by using methods on theReactFlowInstance
this hook returns.
Last updated on