Skip to content

MobX

@input-actions/mobx backs state with a MobX observable.box. The unique value: the same observable that powers useStore is reachable directly via store.getBox(), so autorun, reaction, computed, and mobx-react observer all work on the action-driven state — reactive reads outside React’s lifecycle, declared once, no manual subscribe / unsubscribe plumbing.

Terminal window
pnpm add @input-actions/mobx mobx
import { createMobxStoreSync } from '@input-actions/mobx';
export const store = createMobxStoreSync({
count: 0,
todos: [],
_async: {},
});

createMobxStoreSync returns the concrete MobxAdapter, so store.getBox() needs no cast.

mobx is a static import, so creating the store is synchronous. It works at module scope and during SSR. createMobxStore and ensureMobxLoaded still exist for code that already awaits them, and they resolve immediately.

import { StoreProvider } from '@input-actions/core';
import { store } from './store';
<StoreProvider store={store}>
<App />
</StoreProvider>

Unique benefit — reactive reads outside React

Section titled “Unique benefit — reactive reads outside React”

store.getBox() returns the underlying observable box. Anything MobX-native can observe it:

import { autorun } from 'mobx';
import { store } from './store';
// Updates document.title whenever the count changes — no React component,
// no useEffect, no manual subscribe/unsubscribe.
autorun(() => {
const state = store.getBox().get();
const active = state.todos.filter((t) => !t.done).length;
document.title = active ? `(${active}) Todos` : 'Todos';
});
// Inside a React component, you can mix React subscription (useStore) with
// a MobX reaction that observes the same box — same source of truth.
import { reaction } from 'mobx';
import { useEffect } from 'react';
export function ChangeLogger() {
useEffect(() => {
return reaction(
() => store.getBox().get().todos.length,
(n, prev) => console.log(`todos: ${prev} → ${n}`),
);
}, []);
return null;
}

Because every input-actions dispatch mutates the same observable, the action vocabulary and MobX reactivity are coherent. You’re not paying a synchronization tax between two parallel sources.

  • autorun(fn) — fn re-runs whenever any observable it reads changes. Returns a disposer.
  • reaction(read, effect) — like autorun, but you separately declare what to track and what to do.
  • computed(() => derive(box.get())) — derived values cached against observable reads.
  • mobx-react observer — works on the underlying observable; components that use it re-render on MobX’s terms.
  • MobX devtools — sees the box like any other observable.

The escape hatch is intentional. From the adapter docs:

Adapter-specific extension: the underlying MobX observable.box. Use it with autorun(() => log(box.get())), reaction(() => box.get(), fn), computed(() => derive(box.get())), mobx-react observer, etc. — all of which run on the same backing observable as the action-driven state.

The mobx example wires autorun to document.title and uses a reaction for a separate debug log — both observe the same store that drives the React UI through useStore.