diff --git a/packages/react/src/hooks/useReactFlow.ts b/packages/react/src/hooks/useReactFlow.ts index 3a022b15..6be3f90b 100644 --- a/packages/react/src/hooks/useReactFlow.ts +++ b/packages/react/src/hooks/useReactFlow.ts @@ -3,7 +3,7 @@ import { getElementsToRemove, getOverlappingArea, isRectObject, nodeToRect, type import useViewportHelper from './useViewportHelper'; import { useStoreApi } from './useStore'; -import type { ReactFlowInstance, Instance, Node, Edge } from '../types'; +import type { ReactFlowInstance, Instance, Node, Edge, InternalNode } from '../types'; import { getElementsDiffChanges, isNode } from '../utils'; import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'; @@ -20,13 +20,20 @@ export function useReactFlow>(() => { - return store.getState().nodes.map((n) => ({ ...n })) as NodeType[]; - }, []); + const getNodes = useCallback>( + () => store.getState().nodes.map((n) => ({ ...n })) as NodeType[], + [] + ); - const getNode = useCallback>((id) => { - return store.getState().nodeLookup.get(id)?.internals.userNode as NodeType; - }, []); + const getInternalNode = useCallback>( + (id) => store.getState().nodeLookup.get(id) as InternalNode, + [] + ); + + const getNode = useCallback>( + (id) => getInternalNode(id)?.internals.userNode as NodeType, + [getInternalNode] + ); const getEdges = useCallback>(() => { const { edges = [] } = store.getState(); @@ -299,6 +306,7 @@ export function useReactFlow = { nodes: NodeType[]; @@ -20,6 +20,7 @@ export namespace Instance { ) => void; export type AddNodes = (payload: NodeType[] | NodeType) => void; export type GetNode = (id: string) => NodeType | undefined; + export type GetInternalNode = (id: string) => InternalNode | undefined; export type GetEdges = () => EdgeType[]; export type SetEdges = ( payload: EdgeType[] | ((edges: EdgeType[]) => EdgeType[]) @@ -83,6 +84,13 @@ export type ReactFlowInstance; + /** + * Returns an internal node by id. + * + * @param id - the node id + * @returns the internal node or undefined if no node was found + */ + getInternalNode: Instance.GetInternalNode; /** * Returns edges. *