Minimalist, boilerplate-free, framework-agnostic state container
- Simple - it takes 2 minutes to learn
- Boilerplate-free
- comes with built-in default actions (
set()
,reset()
,toggle()
,merge()
, etc.) - handles loading and error states for async actions for you
- comes with built-in default actions (
- Framework-agnostic
import { actus } from "actus";
import React from "react";
import ReactDOM from "react-dom";
actus({
state: 0,
subscribers: [
({ state, actions }) => {
ReactDOM.render(
<>
<h1>{state}</h1>
<button onClick={actions.increment}>+</button>
<button onClick={actions.decrement}>-</button>
</>,
document.querySelector("#root")
);
},
],
});
import { actus } from "actus";
// actus() returns actions bound to the current state, in case if you need them
// outside of subscribers:
const actions = actus({
// The initial state:
state: { number: 0 },
// Actions accept an optional payload and the current state, and must return
// a new state:
actions: {
number: {
add: ({ state, payload }) => state + payload,
},
},
// Subscribers will be called sequentially during initialization and then
// after any action call:
subscribers: [({ state, actions, actionName, payload }) => {}],
});
// The first argument is the payload:
actions.number.add(1);
The following plugins are enabled automatically (but can be redefined manually if needed):
- defaultActions - basic actions for your state properties (
set()
,reset()
,toggle()
,merge()
, etc.) - logger - logs actions and state changes to your console
- freeze - deep freezes your state to prevent mutations
- actus-redux-devtools - use Redux DevTools Extension with actus
Other plugins that can be enabled manually:
- persist - persists state to a synchronous storage (
localStorage
by default) - actus-state-validator - state validator and normalizer powered by joi
Sources of inspiration: