Usage Basics โ
After installing atomix-core
, you can start managing state using atoms โ simple units of state that hold your data and notify subscribers on change.
๐งฑ Creating an Atom โ
ts
import { createAtom } from "atomix-core";
const counter = createAtom({ count: 0 }, (set) => {
increment: () => set((state) => ({ count: state.count + 1 }));
});
Atoms store state internally and provide a consistent API to read, update, and subscribe to changes.
๐ Reading State โ
ts
const state = counter.getState();
console.log(state.count); // 0
๐ Updating State โ
ts
counter.setState((prev) => ({
count: prev.count + 1,
}));
counter.increment();
Updates are done immutably using a function that receives the previous state.
๐ฏ Subscribing to State โ
ts
const unsubscribe = counter.subscribe(
(state) => state.count, // selector
(value) => console.log("New count:", value) // listener
);
Atomix uses selectors to watch only parts of the state you care about, improving performance and avoiding unnecessary work.
๐งผ Unsubscribing โ
ts
unsubscribe();
Always unsubscribe when the listener is no longer needed (in useEffect cleanup, for example).