v-bucket
Fast, Simple, and Lightweight State Management for Vue 3.0 built with composition API, inspired by Vuex.
Examples
Running the examples:
Running tests
installation
install this package from NPM:
or yarn:
usage
first you need to create your bucket.
create an index.js file, then import createBucket from v-bucket to start creating your store, like this:
then import it in your app entry point, main.js like this:
using with composition api
for using v-bucket inside the setup() function you need to import useBucket first. like this:
using with option api
like vue 2.X apps you can access v-bucket on the vue instance outside of the setup() function. like this:
you only import useBucket when you want to use it inside setup() function
states
this is a single object contains all your application states.
accessing states:
declare states inside your bucket (index.js):
NOTE: states object will be reactive by default. that means they will react to changes and update based on it.
NOTE: it's better to use mutations or getters to access states.
mutations
use mutations when you need to change a state value, all mutations will take 2 parameter:
- state
- payload
with state, you can have access to states object and change its value by passing payload.
NOTE: payload is optional.
you can't access mutations directly. you have to call them with commit method. like this:
getters
you can use getters to access states and their values. getters are like Vue computed methods, they will cache the state and return it only when the state value change.
all getters will take 1 parameter:
- state (used to access states object)
using it in your vue app:
actions
Actions are like mutations. the difference is:
- They can handle asynchronous operations
- Instead of mutating the state, actions commit mutations.
also you can call other actions with context.dispatch. like this:
also you can't access actions directly. you have to call them with dispatch method. like this:
the context parameter will return root instance of our bucket. this is useful when working with modules.
modules
from vuex docs:
- Due to using a single state tree, all states of our application are contained inside one big object. However, as our application grows in scale, the store can get really bloated. To help with that, Vuex allows us to divide our store into modules.
every modules also can have their own states, mutations, actions or even modules
how to access module states
all modules will be installed under the root module. so by accessing the root state you can also have access to its children. like this:
how to access module getters
if you remember we used bracket notation to access getters, you can access your module's getter by defining its path. like this:
they above code represent something like this:
-root
-----moduleA
-----moduleB
how to access module mutations
mutations are like getters, you can access your module's mutation by defining its path. like this:
how to access module actions
actions are like getters, you can access your module's actions by defining its path. like this:
sometimes you need to access another module's mutation or action within the current module
since the context parameter refer to the root module, you can access all of your modules by defining their path. like this:
NOTE: if you want to access a module from another module like example, you need to define your paths from root
plugins
v-bucket store accept the plugins option that exposes hooks for each mutation and actions. plugins will allow you to extend the functionality of v-bucket
a hello world plugin
the function that has the responsibility to be a plugin, need to return another function.
v-bucket will use that function to expose the root module and its hooks
plugin hooks
there are two hooks available:
bucket.onMutation(callback(mutation))
this function will be called after every mutation.
also, it will return information about the mutation that has been fired.
this information contains: name, module and full path of that mutation.
bucket.onAction(callback(action))
this function will be called after every action.
also, it will return information about the action that has been fired.
this information contains: name, module and full path of that action.
usage:
if you want use Class syntaxt, no worries! do something like this:
easy right?
and use it like this in your store:
Since you have access to the bucket root module in the plugins, you also able to commit, dispatch and use getters.