useOnSelectionChange()
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:| Name | Type | Default |
|---|---|---|
params | () => { onChange: OnSelectionChange<NodeType, EdgeType>; }A function that returns the hook parameters |
Notes
- This hook can only be used in a component that is a child of a
<SvelteFlowProvider />or a<SvelteFlow />component.