javascript / intermediate
Snippet
Custom Equality Logic for Signals
By default, Signals use referential equality for objects. By providing a custom equality function, you can prevent unnecessary UI re-renders if the underlying data properties (like an ID) remain the same even if the object reference changes.
snippet.js
1
2
3
userData = signal({ id: 1, name: 'Alex' }, {equal: (a, b) => a.id === b.id});
angular
Breakdown
1
signal({ id: 1, ... }, {
Initializes a signal with an object and an options configuration.
2
equal: (a, b) => a.id === b.id
Defines a custom comparison logic to determine if the signal should notify its consumers.