Skip to Content
参考钩子

useNodesInitialized()

GitHub上的源代码 

这个钩子会告诉你一个流中的所有节点是否都已经被测量并赋予了宽度和高度。当你向流中添加一个节点时,这个钩子会先返回 false,然后在节点被测量后再次返回 true

🌐 This hook tells you whether all the nodes in a flow have been measured and given a width and height. When you add a node to the flow, this hook will return false and then true again once the node has been measured.

import { useReactFlow, useNodesInitialized } from '@xyflow/react'; import { useEffect, useState } from 'react'; const options = { includeHiddenNodes: false, }; export default function useLayout() { const { getNodes } = useReactFlow(); const nodesInitialized = useNodesInitialized(options); const [layoutedNodes, setLayoutedNodes] = useState(getNodes()); useEffect(() => { if (nodesInitialized) { setLayoutedNodes(yourLayoutingFunction(getNodes())); } }, [nodesInitialized]); return layoutedNodes; }

签名

🌐 Signature

Parameters:
NameTypeDefault
options.includeHiddenNodesbooleanfalse
Returns:
boolean

Whether or not the nodes have been initialized by the <ReactFlow /> component and given a width and height.

注释

🌐 Notes

  • 如果内部节点数组为空,此钩子总是返回 false
Last updated on