StoreContext
StoreContext<D> is the first argument of the factory function passed to defineStore. It provides all the APIs needed to set up a store.
Properties
context
context: Container<any>The IoC container associated with this store. Defaults to the parent store’s context (or rootContext for root stores). Use it to fetch dependencies.
store
store: Store<D> | nullReference to the current store instance. Only available after initModel() is called — returns null before that.
Methods
initModel
initModel(initialModel: D["model"]): D["model"]Creates the MobX observable model for this store. Wraps the object with makeAutoObservable. Must be called exactly once per store.
Returns the observable model (same reference as the input, now proxied by MobX).
const model = store.initModel({ value: 0, get computed() { return model.value * 2; }, increment() { model.value++; },});initState
initState<S extends object>(state: S): SCreates private observable state not exposed on the store’s public model. Wraps with makeAutoObservable. Can be called at most once per store.
const state = store.initState({ intervalId: 0, loadCompleted: false,});onInit
onInit(fn: () => void | Promise<void>): voidRegisters a callback called immediately after the store factory returns. Can be sync or async. Must be called at most once.
When async, the store stays in INITIALIZING state until the promise resolves.
store.onInit(async () => { const data = await fetchData(); runInAction(() => { model.items = data; });});onDispose
onDispose(fn: () => void | Promise<void>): voidRegisters a callback called when the store is disposed/unmounted. Can be sync or async. Must be called at most once.
store.onDispose(() => { model.stop(); // clean up resources});mount
mount<SD>(storeFactory: StoreFactory<SD>, params?: SD["params"]): Store<SD>Creates a child store attached to this store. The child inherits this store’s context and will be automatically disposed when the parent is disposed.
const child = store.mount(Counter, { value: 10 });unmount
unmount(store: Store<any> | null): nullDisposes a child store and returns null. Convenient for clearing the reference in one line:
model.child = store.unmount(model.child);terminate
terminate(): voidTriggers the store’s own disposal. Equivalent to the parent calling unmount on this store.
createChildContext
createChildContext(services?: ServiceFactory<any, any>[]): voidCreates a new child IoC container extending the current store.context, and replaces store.context with it. Child stores mounted after this call will inherit the new context.
store.createChildContext([ factory({ provide: [ConfigSIID], load: () => ({ debug: true }) }),]);autorun
autorun( effect: (r: IReactionPublic) => any, options?: IAutorunOptions): IReactionDisposerWrapper over MobX’s autorun that auto-disposes with the store. Sets default name (based on store #id) and onError options.
Must be called after initModel().
store.autorun(() => { params.onChange?.(model.value);});reaction
reaction<T>( expression: (r: IReactionPublic) => T, effect: (arg: T, prev: T | undefined, r: IReactionPublic) => void, options?: IReactionOptions<T>): IReactionDisposerWrapper over MobX’s reaction that auto-disposes with the store. Defaults to fireImmediately: true (unlike plain MobX). The effect function is automatically wrapped in runInAction.
Must be called after initModel().
store.reaction( () => model.value, (value) => { model.message = `Value is ${value}`; },);