ui-router-mobx
ui-router-mobx provides MobX bindings for a @uirouter/core router: an observable store of its current state. It imports only mobx and @uirouter/core, so any MobX reaction can follow route state without Lit. lit-ui-router-mobx builds its Lit controllers on it and re-exports RouterStore.
Installation
npm install ui-router-mobx mobx@uirouter/core and mobx are peer dependencies. Both MobX majors are supported (mobx@^6.0.0 || ^7.0.0), and the store's API is the same on either.
RouterStore
RouterStore.for(router) returns the router's observable store. The first call for a router creates its one store and registers its transition hooks: onSuccess updates the settled fields, and onStart feeds pending and error. Later calls return the same store. The store and the hooks live as long as the router.
Any MobX reaction, autorun or computed that reads the store re-runs when the values it reads change:
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'),
);| Member | Value |
|---|---|
current | the current state declaration |
params | the current parameter values, a fresh object per transition |
transition | the most recent successful transition |
pending | the newest transition still in flight, undefined when none is |
error | the newest transition's RejectType.ERROR rejection, or undefined |
includes(stateOrName, params?) | an observable StateService.includes, globs like 'a.**' included |
Each field is replaced per transition, never mutated, so a reaction compares them by reference.
pending is set when a transition starts. The transition's promise clears it when that transition settles, whether it succeeds, errors or is aborted. A superseded transition never clears its successor, and a transition already running on first use counts as pending. transition still means the most recent successful one.
error holds the newest transition's failure as core's Rejection. Only a RejectType.ERROR rejection, such as a resolve that throws, sets it; superseded, redirected, aborted and ignored transitions leave it undefined. The next transition clears it as it starts, so a retry clears the error as it begins, and a superseded transition that fails late never sets it.
One MobX action writes pending and error together, so a reaction sees a failure as one change and enforceActions sees no unguarded writes.
const dispose = reaction(
() => (store.pending ? 'loading' : store.error ? 'error' : 'idle'),
(status) => (document.body.dataset.routeStatus = status),
);To manage a store yourself, call attach(router) on a new RouterStore(). It returns the hooks' deregistration function, and it throws if the store already mirrors a different router.
Typed stores
RouteTypes is empty, and the store's current is a StateDeclaration, its params are RawParams, and includes takes any StateOrName. Filling it in by module augmentation types the store:
snapshot: a route snapshot type. Itscurrentandparamstype the store's fields, and itsincludesparameter types the targetsincludestakes, such as globs and state objects.snapshots: keyed by state name, the snapshot narrowed to that state's subtree.includes(name)with one of its names is a type predicate that narrows the store'scurrentandparams.
declare module 'ui-router-mobx' {
interface RouteTypes {
snapshot: AppRouteSnapshot;
snapshots: { 'contacts.contact': ContactSnapshot };
}
}Releases
Every release, with its notes and git tag, is on the ui-router-mobx releases page. Compare two versions:
Further reading
- API reference
- lit-ui-router-mobx
- MobX — reactions
- On the Altitude Atlas — this package as a brick in sheet 2's model in the round, sized by its authored files and seated on what it peers