0.2.0 ā¢ Published 5 years ago
statefully v0.2.0
Statefully
š¦ 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