Initializes an Atom instance with a given value.
function that returns the value of the Computed
Optional
options: { Options
A function that compares two values for equality.
Indicates whether the computed is a hard function. If true, it prevents calling the function by comparing the string representation of the dependencies.
The name of the Computed instance.
Private
#cachedPrivate
#currentPrivate
#fnTrue if the reactive item has been destroyed, false otherwise.
Returns the current value of the Computed value without tracking it for dependency management. This is useful when you want to access the value without affecting its reactive state.
The current value of the Computed value.
const a = atom(0);
const b = atom(0);
const c = computed(() => a.value + 1);
const d = computed(() => c.valueUntracked + b.value);
d.subscribe(() => {
console.log(`d = ${d.value}`);
});
console.log(`c = ${c.value}`);
// Outputs: c = 1
a.value++;
// a = 1
// Outputs: nothing
console.log(`c = ${c.value}`);
// Outputs: c = 2
b.value++;
// Outputs: d = 3
Private
#arePrivate
#calcPrivate
#getChecks if two values are equal. If the compareFn property is a function, it is used to compare the two values. If the compareFn property is not a function, the values are compared using the === operator. If the optional second argument is not provided, the value of the reactive item is used.
The first value to compare.
Optional
b: anyThe second value to compare. If not provided, the value of the reactive item is used.
True if the two values are equal, false otherwise.
Returns true if there has been an error while calculating the value of the reactive item, false otherwise. This method returns true if the reactive item has been destroyed, if the reactive item has an error, or if the calculation of the value of the reactive item has thrown an error.
Whether there has been an error while calculating the value of the reactive item.
Checks whether the Computed value needs to be recalculated. A recalculation is needed if the engine's shouldRecalc property is true, if the engine has an error, or if the version string of the dependencies has changed.
true if the Computed value needs to be recalculated, false if it does not.
Subscribes a function to be called when the reactive item is destroyed. The function is called with no arguments.
The function to be called.
A function that unsubscribes the given function.
Subscribes a function to be called whenever a subscriber is added to the reactive item. The function is called with no arguments.
The function to be called.
A function that unsubscribes the given function.
Subscribes a function to be called whenever there are no longer any subscribers. The function is called with no arguments.
The function to be called.
A function that unsubscribes the given function.
Subscribes a function to be called whenever the value of this reactive item changes.
The function to be called whenever the value of this reactive item changes.
Optional
options: { Optional options.
The delay in milliseconds before the function is called.
The signal to abort the subscription.
Computed is a reactive primitive that holds a value that is computed from other reactive values. It is the base unit of reactive state.
Example