EdgeChangeset
An EdgeChangeset is what React Flow passes your onEdgesChange
handler whenever edges change. It holds the EdgeChange objects and comes with an applyTo() method, to apply the changes.
import type { Edge, EdgeChangeset } from '@xyflow/react';
function onEdgesChange(changes: EdgeChangeset<Edge>) {
setEdges((eds) => changes.applyTo(eds));
}You only need to handle this yourself in a controlled flow (you pass edges and
onEdgesChange). useEdgesState already calls
changes.applyTo() for you.
Reading changes
A changeset is iterable, and it can hand you changes by type or by edge — so inspecting an update doesn’t mean scanning an array yourself.
// every change, in order
for (const change of changes) {
console.log(change.type, change.id);
}
// just the removals
if (changes.getByType('remove').length > 0) {
saveUndoSnapshot();
}
// everything queued for one edge
const changesForEdge = changes.getForEdge('e1-2');Editing before you apply
Because the changeset is a real object you can drop or add changes before applying it — handy for vetoing an update without rebuilding the array.
function onEdgesChange(changes: EdgeChangeset<Edge>) {
// keep this edge no matter what the interaction asked for
changes.removeForEdge('e1-2');
setEdges((eds) => changes.applyTo(eds));
}Members
| Member | Signature | Description |
|---|---|---|
applyTo | (edges: EdgeType[]) => EdgeType[] | Applies every queued change and returns the next array. |
add | (change: EdgeChange | EdgeChange[]) => void | Queues one or more changes. |
remove | (change: EdgeChange) => void | Removes a queued change by object reference. |
getByType | (type: EdgeChange['type']) => EdgeChange[] | Every queued change of that type, e.g. 'select' or 'remove'. |
getForEdge | (edgeId: string) => EdgeChange[] | Every queued change for one edge. |
removeForEdge | (edgeId: string) => void | Drops every queued change for one edge. |
toArray | () => EdgeChange[] | All queued changes as a flat array. |
size | number | How many edges the changeset touches. |
version | number | Bumped on every edit. |
The changeset also implements Symbol.iterator, so [...changes] and for…of work directly.
Notes
- Construct one yourself with
new EdgeChangeset(changes?)— for example to replay recorded changes. - The standalone
applyEdgeChanges(changes, edges)util is removed. Usechanges.applyTo(edges)instead. - Nodes have the mirrored
NodeChangeset.