ui-router-mobx
v0.3.0
Published
MobX bindings for UI-Router: a framework-agnostic observable store of the router's current state
Readme
ui-router-mobx
MobX bindings for UI-Router: a framework-agnostic observable store of the router's current state. It imports only mobx and @uirouter/core.
lit-ui-router-mobx builds Lit controllers on it and re-exports RouterStore.
Installation
npm install ui-router-mobx mobxFeatures
RouterStore.for(router): the observable store for a router. There is one store, and oneonSuccessand oneonStarthook, per router, attached lazily on first use.current,paramsandtransition: the current state declaration, a fresh plain copy of the params, and the most recent successful transition. Each is replaced per transition, never mutated, so reactions compare them by reference.pendinganderror: the newest transition still in flight, and that transition'sRejectType.ERRORrejection;undefinedotherwise.pendingis set on start and cleared when that transition settles, however it settles, unless a newer one has started;erroris cleared when the next transition starts. One MobX action writes both.includes(stateOrName, params?): an observableStateService.includes, glob patterns such as'admin.**'included.RouteTypes: an empty interface an app or a registry fills in by module augmentation. Itssnapshotmember typescurrent,paramsand the targetsincludestakes, and itssnapshotsmember, keyed by state name, letsincludes(name)narrowcurrentandparamsto that state's subtree. Empty, the store's types areStateDeclaration,RawParamsandStateOrName.attach(router): for managing anew RouterStore()yourself. It returns the hooks' deregistration function and throws if the store already mirrors a different router.
Usage
Any MobX reaction, autorun or computed that reads the store re-runs when the values it reads change, with no transition hooks of its own:
import { reaction } from 'mobx';
import { RouterStore } from 'ui-router-mobx';
const store = RouterStore.for(router);
const dispose = reaction(
() => store.includes('admin.**'),
(inAdmin) => console.log(inAdmin ? 'entered admin' : 'left admin'),
);Loading and error state
const dispose = reaction(
() => (store.pending ? 'loading' : store.error ? 'error' : 'idle'),
(status) => (document.body.dataset.routeStatus = status),
);MobX 6 and 7
Both majors are supported (mobx@^6.0.0 || ^7.0.0) and both are exercised in CI. The store's own API is identical on either.
Dependencies
@uirouter/core and mobx are peer dependencies. lit and lit-ui-router are not, and the bundleProbe claim in package.json holds the entry free of them.
