300
stars
26
forks
8
watchers
50
prs
22
issues
48
releases
README
USM
!Node CI
USM is a universal state modular library, supports Redux(5.x), MobX(6.x), Vuex(4.x+), Pinia(3.x) and Angular(2.0+).
Motivation
usm provides a generic state model that is class-first, which help us to be OOP at almost no cost and is compatible with the ecology of every state library.
When you don't want to learn the paradigm of any state library, usm can help you use any state library. When your project's business code is based on usm, the architecture will be more flexible.
Support
| Libraries/Frameworks | None/Angular2+ | Redux | MobX | Vuex | Pinia |
| :--------------------------------------------------------- | :--------------------------------------: | :--------------------------------------------------: | :------------------------------------------------: | :------------------------------------------------: | :--------------------------------------------------: |
| Package Name | usm | usm-redux | usm-mobx | usm-vuex | usm-pinia |
| Integrated Mutative | ✅ | ✅ | 🚫 | 🚫 | 🚫 |
| State Type | Immutable | Immutable | Observable | Observable | Observable |
Installation
To install usm:
bash
pnpm add usm npm install --save usm
And if you want to use Redux/MobX/Vuex/Pinia, you just install usm-redux/usm-mobx/usm-vuex/usm-pinia.
usm and usm-redux depend on Mutative 2.x (minimum 2.0.2). Mutative's builds target ES2018; transpile that dependency when targeting older browsers. See the Mutative v2 migration guide for changes to patch behavior and runtime checks.
Usage
- Use @state to decorate a module state.
- Use @action to decorate a module method for state changes.
- Use createStore to create a store.
ts
import { state, action, createStore } from 'usm';
// You can also use usm-redux, usm-mobx, usm-vuex or usm-pinia.
class Counter {
@state
count = { sum: 0 };
@action
increase() {
this.count.sum += 1;
}
}
const counter = new Counter();
const store = createStore({
modules: [counter],
});
counter.increase();
const newState = Object.values(store.getState())[0] as Counter;
expect(newState.count).toEqual({ sum: 1 });
Examples
- React
- Vue
APIs
@state
Define a shared state for a module, and you can use @state for decoration. When useLatest release
4.9.0 v4.9.0 2025-10-08Full Changelog: https://github.com/unadlib/usm/compare/v4.8.2...v4.9.0