Skip to content

EvgenyOrekhov/actus

Repository files navigation

actus

Minimalist, boilerplate-free, framework-agnostic state container

npm version

  • 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
  • Framework-agnostic

Examples

React Counter App

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")
      );
    },
  ],
});

Try it on CodeSandbox

API

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);

Plugins

The following plugins are enabled automatically (but can be redefined manually if needed):

Other plugins that can be enabled manually:

Frameworks

Acknowledgements

Sources of inspiration:

License

MIT