detectChangedProps

function
utils/detectChangedProps.ts:128
signature
detectChangedProps(oldState, newState, path, ancestors): string[]

Computes the list of **dotted leaf paths** that changed between two values. The algorithm performs a deep structural comparison with special handling for: - **Primitives / null** → treated as leafs (change = current path; two NaNs are equal) - **Date** → compares getTime() - **RegExp** → compares source and flags - **Map, Set and binary values** (typed arrays, DataView, ArrayBuffer) → one value at their own path, compared by reference - **Arrays** → compared element by element, producing paths like "items.0.title"; when the lengths differ, the array path itself is reported too, along with each index present on one side only - **Objects** → compares by the **union of keys**, recursing into shared keys and marking added/removed keys as changed at their **full path** Cycles are handled by tracking the (old, new) pairs currently on the **recursion path** (added on entry, removed on unwind). A pair is skipped only when it is a genuine ancestor of itself (a real cycle), a pair that merely appears again at a *sibling* path (legitimate aliasing, e.g. the same object referenced from two keys) is still diffed, so real changes at the second site are never dropped.

Parameters

NameTypeDescription
oldStateanyPrevious value to diff.
NameTypeDescription
newStateanyNext value to diff.
NameTypeDescription
pathstringCurrent dotted path (callers pass "" for root; recursion appends segments).
NameTypeDescription
ancestorsMap<object, Set<object>>(Advanced) Pairs on the current recursion path, for cycle detection. You generally never pass this.

Returns

string[], An array of **dotted leaf paths** that changed. Paths use "." as a separator and indices for arrays (e.g., "todos.0.title"). If nothing changed, returns [].

Example

example
detectChangedProps(
  { user: { name: 'Ada', age: 37 } },
  { user: { name: 'Grace', age: 37 } }
);
// => ['user.name']

Example

example
detectChangedProps(
  { items: [{ title: 'A' }, { title: 'B' }] },
  { items: [{ title: 'A+' }, { title: 'B' }] }
);
// => ['items.0.title']

Example

example
detectChangedProps({ nums: [1,2] }, { nums: [1,2,3] });
// => ['nums', 'nums.2']
detectChangedProps({ nums: [1,2,3] }, { nums: [0,2] });
// => ['nums', 'nums.0', 'nums.2']

Example

example
detectChangedProps(new Date(0), new Date(0), 'createdAt');      // => []
detectChangedProps(new Date(0), new Date(1), 'createdAt');      // => ['createdAt']
detectChangedProps(/a/i, /a/i, 'pattern');                      // => []
detectChangedProps(/a/i, /a/g, 'pattern');                      // => ['pattern']