AtomixStore<T>
β
The AtomixStore<T>
is the core object returned by createAtom
.
It gives you full control over the atomβs state β read, update, and subscribe.
π§± Interface β
ts
interface AtomixStore<T> {
getState(): T;
setState(updater: (prev: T) => T): void;
subscribe<U>(
selector: (state: T) => U,
listener: (selected: U) => void
): () => void;
}
π Methods β
getState() Returns the current state of the atom.
ts
const state = atom.getState();
setState(updater) Updates the state immutably using a pure function
ts
atom.setState((prev) => ({
...prev,
count: prev.count + 1,
}));
The updater receives the previous state and returns the new one.
subscribe(selector, listener) Listens for changes in selected parts of the state.
ts
const unsubscribe = atom.subscribe(
(s) => s.count,
(newCount) => {
console.log("Count changed:", newCount);
}
);
β The listener only runs if the selector value changes (shallow equality).
To stop listening:
ts
unsubscribe();
β Best Practices β
Always use selectors for performance
Use getState for reads, not direct state access
Donβt mutate state β return a new object in setState