+ *
Selected nodes: {selectedNodes.join(', ')}
+ *
Selected edges: {selectedEdges.join(', ')}
+ *
+ * );
+ *}
+ *```
+ *
+ * @remarks You need to memoize the passed `onChange` handler, otherwise the hook will not work correctly.
*/
export function useOnSelectionChange({ onChange }: UseOnSelectionChangeOptions) {
const store = useStoreApi();
diff --git a/packages/react/src/hooks/useOnViewportChange.ts b/packages/react/src/hooks/useOnViewportChange.ts
index 61e4d64f..c82eb344 100644
--- a/packages/react/src/hooks/useOnViewportChange.ts
+++ b/packages/react/src/hooks/useOnViewportChange.ts
@@ -10,12 +10,30 @@ export type UseOnViewportChangeOptions = {
};
/**
- * Hook for registering an onViewportChange handler.
+ * The `useOnViewportChange` hook lets you listen for changes to the viewport such
+ *as panning and zooming. You can provide a callback for each phase of a viewport
+ *change: `onStart`, `onChange`, and `onEnd`.
*
* @public
* @param params.onStart - gets called when the viewport starts changing
* @param params.onChange - gets called when the viewport changes
* @param params.onEnd - gets called when the viewport stops changing
+ *
+ * @example
+ * ```jsx
+ *import { useCallback } from 'react';
+ *import { useOnViewportChange } from '@xyflow/react';
+ *
+ *function ViewportChangeLogger() {
+ * useOnViewportChange({
+ * onStart: (viewport: Viewport) => console.log('start', viewport),
+ * onChange: (viewport: Viewport) => console.log('change', viewport),
+ * onEnd: (viewport: Viewport) => console.log('end', viewport),
+ * });
+ *
+ * return null;
+ *}
+ *```
*/
export function useOnViewportChange({ onStart, onChange, onEnd }: UseOnViewportChangeOptions) {
const store = useStoreApi();
diff --git a/packages/react/src/hooks/useReactFlow.ts b/packages/react/src/hooks/useReactFlow.ts
index 3d857cfa..57b2e001 100644
--- a/packages/react/src/hooks/useReactFlow.ts
+++ b/packages/react/src/hooks/useReactFlow.ts
@@ -20,10 +20,33 @@ import type { ReactFlowInstance, Node, Edge, InternalNode, ReactFlowState, Gener
const selector = (s: ReactFlowState) => !!s.panZoom;
/**
- * Hook for accessing the ReactFlow instance.
+ * 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.
*
* @public
* @returns ReactFlowInstance
+ *
+ * @example
+ * ```jsx
+ *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 (
+ *