Skip to content

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).

v1.0.2 โ€ข Released under the MIT License.