Getting started
mobx-swiss-knife is a collection of small MobX entities for state commonly repeated in applications: tabs, steps, pagination, timers, themes, and lazy loading.
Installation
bash
npm install mobx-swiss-knifebash
yarn add mobx-swiss-knifebash
pnpm add mobx-swiss-knifeUsage
ts
import {
createTabManager,
createTicker,
createTime,
} from 'mobx-swiss-knife';
const tabs = createTabManager({
tabs: [
{ id: 'foo', label: 'Foo' },
{ id: 'bar', label: 'Bar' },
],
fallbackTab: 'foo',
});
const time = createTime({ updatePer: 1000 });
console.log(tabs.activeTab, time.value);Choosing a Utility
- Need section selection:
TabManager. - Need a wizard or checkout flow:
Stepper. - Need
page,pageSize, and conversion tooffset/limit:Paginator. - Need a countdown:
DatesComparator; current time:Time. - Need debounce/throttle:
Timers; a periodic counter:Ticker. - Need keyboard shortcuts:
KeyboardHandler; WebSocket:Socket. - Need a lazy model or Faker:
ModelLoaderandFakerLoader. - Need a theme or settings persistence:
TwoColorThemeStoreandStorage.
Lifecycle
Most entities have destroy(). Call it when destroying the ViewModel or component if the instance is created manually. For reactions and timers, you can pass a shared AbortSignal:
ts
const controller = new AbortController();
const time = createTime({ abortSignal: controller.signal });
const ticker = createTicker({ ticksPer: 1000, abortSignal: controller.signal });
controller.abort(); // stops both instances