Quick Start
The shortest path to a useful Store instance.
Quick Start
One Store can be running after six steps.
1. Install
npm install @ilokesto/store2. Create
import { Store } from "@ilokesto/store";
const counterStore = new Store({ count: 0 });The constructor receives the initial state value. That value is also what getInitialState() returns later.
3. Read
const current = counterStore.getState();
console.log(current.count);Reads are synchronous. getState() returns the current state reference typed as Readonly<T>.
4. Update
counterStore.setState((prev) => ({
...prev,
count: prev.count + 1,
}));setState replaces the whole state value. When your state is an object, copy the fields you want to keep.
You can also pass the next value directly:
counterStore.setState({ count: 10 });5. Subscribe
const unsubscribe = counterStore.subscribe(() => {
console.log("New count:", counterStore.getState().count);
});The listener runs after a successful state replacement. If Object.is says the next value is the same as the current value, the store bails out and does not notify listeners.
6. Unsubscribe
unsubscribe();Call the returned function when the listener is no longer needed. This is especially important in adapters, components, and long running services.
Next steps
- Learn the model behind snapshots, updates, subscriptions, and mutation caveats in Core Concepts.
- Check exact method names and signatures in API Reference.
- Match subscription cleanup to framework lifecycles in Framework Usage.