Cratebox
Cratebox is a plugin based state management library.
- Declarative: with cratebox you will create almost effortless stores to keep your state in a global level.
- Plugin-based: create your own plugins to handle state, views & actions the way you want or let Cratebox use it's defaults so you don't have to worry about anything.
Installation
Cratebox can be installer into your project with yarn or npm with the following commands:
yarn add cratebox
npm install cratebox
Usage & API
Cratebox comes with one named export called createStore which is a function with the following signature:
Let's proceed to describe the signature a little.
- createStore: will take three parameters of types T, V and A which will be used to infer types to give you a good developer experience when using vscode.
- state: will be one of the following types:
- object: a regular object type shape to manage state.
- views: views are a function of state and are used to create computed properties.
- actions: actions are a function of state and should be used to update state from within this function. Note that this is not a rule but it's recommended that you only update the state though actions so you can keep track of where and when you're applying changes to a store. The default plugin that comes with Cratebox will ensure that you don't apply changes directly to state at any point and only through actions.
- plugin: plugins are a way to handle your own state, views & actions logic for the given store. If you don't pass any plugin at the creation time, Cratebox will use it's default plugin for you.
Examples
The following example shows how to create a simple user store to keep some user data within it, make use of views and actions to dispatch changes to the state.
; /** * Define this store's shape! */; /** * User state object that matches the User State Type */; /** * Define User Views * – it's like computed properties! */; /** * Define User Actions * – You'll be able to change the state within this actions only! */; /** * Create the store * and then export it to use it somewhere else! */; ;
Plugins
With cratebox you have the ability to create your own plugins to handle state, views & actions in your own way. This gives you (the developer) the flexibility of using it's own business logic the way you see fit.
The following example comes with a very simple implementation of a plugin for a store with an object type state based. You can use this as a base for your plugin and enhance it the way you consider better.
; /** * Define this store's shape! */; /** * User state object that matches the User State Type */; /** * Define User Views * – it's like computed properties! */; /** * Define User Actions * – You'll be able to change the state within this actions only! */; /** * Create the store * and then export it to use it somewhere else! */;