Skip to Content
Svelte Flow 2 is out! Read the announcement →
ReferenceHooks

useOnSelectionChange()

Source on GitHub 

This hook lets you listen for changes to both node and edge selection. As the name implies, the callback you provide will be called whenever the selection of either nodes or edges changes.

Component.svelte
<script> import { useOnSelectionChange } from '@xyflow/svelte'; let selectedNodes = $state.raw([]); let selectedEdges = $state.raw([]); useOnSelectionChange(() => ({ onChange: ({ nodes, edges }) => { selectedNodes = nodes.map((node) => node.id); selectedEdges = edges.map((edge) => edge.id); }, })); </script> <div> <p>Selected nodes: {selectedNodes.join(', ')}</p> <p>Selected edges: {selectedEdges.join(', ')}</p> </div>

Signature

Parameters:
NameTypeDefault
params() => { onChange: OnSelectionChange<NodeType, EdgeType>; }

A function that returns the hook parameters

Returns:
void

Notes