MCPcopy
hub / github.com/xyflow/xyflow / useReactFlow

Function useReactFlow

packages/react/src/hooks/useReactFlow.ts:58–315  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

56 *```
57 */
58export function useReactFlow<NodeType extends Node = Node, EdgeType extends Edge = Edge>(): ReactFlowInstance<
59 NodeType,
60 EdgeType
61> {
62 const viewportHelper = useViewportHelper();
63 const store = useStoreApi();
64 const batchContext = useBatchContext();
65 const viewportInitialized = useStore(selector);
66
67 const generalHelper = useMemo<GeneralHelpers<NodeType, EdgeType>>(() => {
68 const getInternalNode: GeneralHelpers<NodeType, EdgeType>['getInternalNode'] = (id) =>
69 store.getState().nodeLookup.get(id) as InternalNode<NodeType>;
70
71 const setNodes: GeneralHelpers<NodeType, EdgeType>['setNodes'] = (payload) => {
72 batchContext.nodeQueue.push(payload as NodeType[]);
73 };
74
75 const setEdges: GeneralHelpers<NodeType, EdgeType>['setEdges'] = (payload) => {
76 batchContext.edgeQueue.push(payload as EdgeType[]);
77 };
78
79 const getNodeRect = (node: NodeType | { id: string }): Rect | null => {
80 const { nodeLookup, nodeOrigin } = store.getState();
81
82 const nodeToUse = isNode<NodeType>(node) ? node : nodeLookup.get(node.id)!;
83 const position = nodeToUse.parentId
84 ? evaluateAbsolutePosition(nodeToUse.position, nodeToUse.measured, nodeToUse.parentId, nodeLookup, nodeOrigin)
85 : nodeToUse.position;
86
87 const nodeWithPosition = {
88 ...nodeToUse,
89 position,
90 width: nodeToUse.measured?.width ?? nodeToUse.width,
91 height: nodeToUse.measured?.height ?? nodeToUse.height,
92 };
93
94 return nodeToRect(nodeWithPosition);
95 };
96
97 const updateNode: GeneralHelpers<NodeType, EdgeType>['updateNode'] = (
98 id,
99 nodeUpdate,
100 options = { replace: false }
101 ) => {
102 setNodes((prevNodes) =>
103 prevNodes.map((node) => {
104 if (node.id === id) {
105 const nextNode = typeof nodeUpdate === 'function' ? nodeUpdate(node) : nodeUpdate;
106 return options.replace && isNode(nextNode) ? (nextNode as NodeType) : { ...node, ...nextNode };
107 }
108
109 return node;
110 })
111 );
112 };
113
114 const updateEdge: GeneralHelpers<NodeType, EdgeType>['updateEdge'] = (
115 id,

Callers 15

ControlsComponentFunction · 0.90
useOnInitHandlerFunction · 0.90
useGlobalKeyHandlerFunction · 0.90
SetNotesBatchingFlowFunction · 0.90
ControlledUncontrolledFunction · 0.90
CustomNodeFlowFunction · 0.90
BasicFlowFunction · 0.90
FlowFunction · 0.90
UpdateNodeFunction · 0.90
UseZoomPanHelperFlowFunction · 0.90
BasicFlowFunction · 0.90
SidebarFunction · 0.90

Calls 15

useStoreApiFunction · 0.90
useBatchContextFunction · 0.90
useStoreFunction · 0.90
getElementsToRemoveFunction · 0.90
isRectObjectFunction · 0.90
nodeToRectFunction · 0.90
getOverlappingAreaFunction · 0.90
getNodesBoundsFunction · 0.90
withResolversFunction · 0.90
useViewportHelperFunction · 0.85
onDeleteFunction · 0.85
getInternalNodeFunction · 0.70

Tested by

no test coverage detected