vue-storer
TypeScript icon, indicating that this package has built-in type declarations

0.6.0 • Public • Published

vue-storer

State management for Vue.

Installation

Install vue-storer with your favorite package manager:

$ npm i vue-storer
# or
$ yarn add vue-storer
# or
$ pnpm i vue-storer
# or
$ bun add vue-storer

Usage

To use vue-storer, import the defineStore function:

// esm
import { defineStore } from 'vue-storer';

// cjs
const { defineStore } = require('vue-storer');

Guide

vue-storer provides a defineStore function that allows you to define a store with state, getters, and actions. Here's an example:

import { reactive, readonly } from 'vue';
import { defineStore } from 'vue-storer';

export const useCounter = defineStore('counter', () => {
  const state = reactive({
    name: 'Counter',
    count: 0,
  });

  const getters = readonly({
    doubleCount: computed(() => state.count * 2),
  });

  const actions = readonly({
    increment() {
      state.count += 1;
    },
  });

  return { state, getters, actions };
});

In the example above, defineStore is called with a name (counter) and a factory function that returns an object with state, getters, and actions.

You can then use the store in a Vue component by calling useCounter:

<script lang="ts" setup>
import { useCounter } from './store';

const { state, getters, actions, $reset, $subscribe } = useCounter();

$subscribe((state) => {
  console.log(state.count);
});
</script>

<template>
  <p>Name: {{ state.name }}</p>
  <p>Count: {{ state.count }}</p>
  <p>Double Count: {{ getters.doubleCount }}</p>
  <button @click="actions.increment">Increment</button>
  <button @click="$reset">Reset</button>
</template>

In the example above, useCounter returns an object with state, getters, actions, $reset, and $subscribe. state, getters, and actions are the same as the object returned by the factory function. $reset is a function that resets the store's state to its initial state. $subscribe is a function that allows you to subscribe to changes in the store's state.

Persisted State

If you need persistent data storage, it can be integrated with Web Storage.

import { reactive, readonly } from 'vue';
import { defineStore } from 'vue-storer';

export const useCounter = defineStore(
  'counter', // This will be the stored key.
  () => {
    /* ... */
    return { state, getters, actions };
  },
  sessionStorage, // You can also use `localStorage`.
);

Package Sidebar

Install

npm i vue-storer

Weekly Downloads

7

Version

0.6.0

License

MIT

Unpacked Size

6.83 kB

Total Files

6

Last publish

Collaborators

  • shyam-chen