---
url: https://lit-ui-router.dev/packages/ui-router-mobx.md
description: >-
  A framework-agnostic MobX observable store of the router's current state with
  ui-router-mobx
---

# ui-router-mobx

[`ui-router-mobx`](https://npmx.dev/package/ui-router-mobx) provides
[MobX](https://mobx.js.org) 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`](/packages/lit-ui-router-mobx) builds its Lit
controllers on it and re-exports `RouterStore`.

## Installation

```bash
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)`](/reference/ui-router-mobx/classes/RouterStore#for)
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:

```ts
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.

```ts
const dispose = reaction(
  () => (store.pending ? 'loading' : store.error ? 'error' : 'idle'),
  (status) => (document.body.dataset.routeStatus = status),
);
```

To manage a store yourself, call
[`attach(router)`](/reference/ui-router-mobx/classes/RouterStore#attach) on a
`new RouterStore()`. It returns the hooks' deregistration function, and it
throws if the store already mirrors a different router.

## Typed stores

[`RouteTypes`](/reference/ui-router-mobx/interfaces/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. Its `current` and `params` type the
  store's fields, and its `includes` parameter types the targets
  `includes` takes, 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's `current` and `params`.

```ts
declare module 'ui-router-mobx' {
  interface RouteTypes {
    snapshot: AppRouteSnapshot;
    snapshots: { 'contacts.contact': ContactSnapshot };
  }
}
```

## Releases

## Further reading

* [API reference](/reference/ui-router-mobx/)
* [Releases](/releases/ui-router-mobx/){target="\_self"} — every version, newest first, with its release notes
* [lit-ui-router-mobx](/packages/lit-ui-router-mobx)
* [MobX — reactions](https://mobx.js.org/reactions.html)
* [Altitude Atlas](https://atlas.lit-ui-router.dev/bricks/?focus=ui-router-mobx) — package size and dependency map
