npm.io
0.2.0 • Published 7 years ago

statefully

Licence
MIT
Version
0.2.0
Deps
0
Size
10 kB
Vulns
0
Weekly
0
Stars
2

Statefully

Travis (.org) size size dependencies

A tiny state container for your javascript apps

  • Simple: Easy to learn & use APIs
  • Small: Zero dependencies + 1kb minified size only
  • Pluggable: Use anywhere you want
  • Type-Safe: Great type support with TypeScript & Flow

NOTE: This project is under active development!


Installation

NodeJS

# Using npm
npm install statefully

# Using yarn
yarn add statefully

HTML

<script src="https://unpkg.com/statefully@latest/dist/index.min.js"></script>

Usage

Create container

// Using ES Module
import { createContainer } from "statefully";
// Using CommonJS
const { createContainer } = require("statefully");

// Create container with initial value
const store = createContainer({ greeting: "John" });

Get container state

store.getState(); // { greeting: "John" }

Actions

// Create action
const setGreeting = store.action((state, { name }) => ({ greeting: name }));

// Call action
setGreeting({ name: "Doe" });

store.getState(); // { greeting: "Doe" }

Subscribe container

store.subscribe(() => {
  console.log("Store changed: ", store.getState());
});

Guide

With TypeScript

// Create container
type State = { greeting: string };
const store = createContainer<State>({ greeting: "John" });

// Create action
type SetGreetingProps = { name: string };
const setGreeting = store.action<SetGreetingProps>((state, { name }) => ({
  greeting: name,
}));

// Call action
setGreeting({ name: "Doe" });

Features
  • Actions
  • Merge actions
  • Subscription
  • TypeScript Support
  • Flow Support
  • React binding

License

MIT

Keywords