Initializes an Atom instance with a given value.
The initial value of the Atom.
Optional
options: { Options.
A function that compares two values for equality.
The name of the Atom.
Private
#currentTrue if the reactive item has been destroyed, false otherwise.
Returns the current value of the Atom. If the engine is destroyed, an error is thrown.
The current value of the Atom.
Sets the value of the Atom. If the new value is the same as the current value, no action is taken. Updates the current value to the new value if they are different.
The new value to set for the Atom.
Returns the current value of the Atom 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 Atom.
const a = atom(0);
const b = atom(0);
const c = computed(() => a.value + b.valueUntracked, {
name: "c",
});
c.subscribe(() => {
console.log(c.name, c.value);
});
console.log("change b.value");
b.value++;
b.value++;
console.log("change a.value");
a.value++;
// Output: c 3
a.value++;
// Output: c 4
Checks 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.
Retrieves the current value of the Atom. If the engine is destroyed, an error is thrown. Tracks the Atom for dependency management.
Optional
options: { Optional options. If untracked
is false
, the Atom value will be added to the getValueTracker.
Optional
untracked?: booleanThe current value of the Atom.
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.
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.
Atom is a reactive primitive that holds a value. It is the base unit of reactive state.
Example