Core API reference (@tanstack/store re-exports)
src/index.ts:1 does export * from '@tanstack/store', so every symbol below is importable from @tanstack/vue-store in Vue apps. Official reference: API index.
createStore
createStore<T>(getValue: (prev?) => T): ReadonlyStore<T> // derived
createStore<T>(initialValue: T): Store<T> // simple
createStore<T, TActions>(initialValue: NonFunction<T>,
actions: StoreActionsFactory<T, TActions>): Store<T, TActions> // with actions- Passing a function creates a derived
ReadonlyStore. It recomputes when dependencies change;prevreceives the previous computed value, useful for accumulation. - The actions factory receives
{ setState, get }and returns an actions object exposed as typedstore.actions.
const counter = createStore(0)
const derived = createStore(() => counter.state * 2)
const petStore = createStore({ cats: 0 }, ({ setState }) => ({
addCat: () => setState((s) => ({ ...s, cats: s.cats + 1 })),
}))Store and ReadonlyStore
Store docs · ReadonlyStore docs
Store<T, TActions = never>:
stategetter andget(): read current value.setState(updater: (prev) => T): immutable update; the updater receives previous state.subscribe(observerOrFn): Subscription: observer function or RxJS-style observer object; returns{ unsubscribe }. This replaced the removedEffectclass in v0.9.0.actions: bound, typed actions when created with an actions factory.
ReadonlyStore<T> implements Store without setState and actions. Derived stores return this type.
createAtom
createAtom<T>(getValue: (prev?) => T, options?: AtomOptions<T>): ReadonlyAtom<T> // computed
createAtom<T>(initialValue: T, options?: AtomOptions<T>): Atom<T> // writable- Writable
Atom<T>exposesset, which accepts a value or an updater function (set: (fn) => void & (value) => void). - Computed atoms return
ReadonlyAtom<T>withoutset. AtomOptions.compare?: (prev, next) => booleancontrols change detection. AtomOptions docs
createAsyncAtom
createAsyncAtom<T>(getValue: () => Promise<T>, options?): ReadonlyAtom<AsyncAtomState<T, unknown>>- Wraps a promise in an atom whose value is a status discriminated union:
{ status: 'pending' } | { status: 'done'; data: T } | { status: 'error'; error: unknown }. - Consume with
useSelector(atom)and branch onstatus; no hand-rolled loading flags. - The
AsyncAtomStatetype itself is not exported; useReturnTypeor infer from the atom if the shape is needed.
batch and flush
batch(() => { /* grouped updates */ }): void
flush(): voidbatch(fn)groups updates insidefninto one notification cycle; subscribers are notified once with the final state.flush()drains pending effects immediately. It is a no-op insidebatch.
shallow
shallow<T>(objA: T, objB: T): boolean- Compare order:
Object.is, then Map and Set contents, Date timestamps, then own keys including symbols. - Main use:
{ compare: shallow }in the Vue hooks when a selector builds fresh objects or arrays. - Caution: objects with only property getters compare unequal even when contents match (issue #218, open). Symbol-key comparison was fixed in #226.
- Dates:
shallowcompares timestamps, not identity. Under the default===compare, a newDatereference is always a change; see issue #160.
toObserver
Normalizes a callback or RxJS-style observer ({ next, error, complete }) for subscribe.
Exported types
From the reference index:
- Classes:
Store,ReadonlyStore. - Interfaces:
Atom,AtomOptions,BaseAtom,InteropSubscribable,Readable,ReadonlyAtom,Subscribable,Subscription(plus internalInternalBaseAtom,InternalReadonlyAtom). - Type aliases:
AnyAtom,Observer,Selection,StoreAction,StoreActionMap,StoreActionsFactory.