npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-use-state

v0.0.8

Published

A wrapper for the Vue reactivity APIs you love, making them global and easily accessible across the app. It ensures that the APIs retain their natural behavior while being fully typed for a great user experience.

Downloads

517

Readme

Vue Use State

A wrapper for the Vue reactivity APIs you love, making them global and easily accessible across the app. It ensures that the APIs retain their natural behavior while being fully typed for a great user experience. 💚

npm version

Installation

Using Bun

bun add vue-use-state

Using NPM

npm install vue-use-state

Using Yarn

yarn add vue-use-state

Using PNPM

pnpm add vue-use-state

Usage

import { useState } from 'vue-use-state';

const key1 = useState('1', 0); // Ref<number>
const key2 = useState('2', () => 0); // Ref<number>
const key3 = useState('3', () => ref(0)); // Ref<number>
const key4 = useState('4', () => ({ a: 1 })); // Ref<{ a: number }>
const key5 = useState('5', reactive({ a: 1 })); // Ref<{ a: number }>
const key6 = useState('6', () => reactive({ a: 1 })); // Ref<{ a: number }>
const key7 = useState('7', readonly({ a: 1 })); // Ref<{ readonly a: number }>
const key8 = useState('8', () => readonly({ a: 1 })); // Ref<{ readonly a: number }>
const key9 = useState('9', shallowReactive({ a: 1 })); // Ref<ShallowReactive<{ a: number }>>
const key10 = useState('10', () => shallowReactive({ a: 1 })); // Ref<ShallowReactive<{ a: number }>>
const key11 = useState('11', shallowReadonly({ a: 1 })); // Ref<Readonly<{ a: number }>>
const key12 = useState('12', () => shallowReadonly({ a: 1 })); // Ref<Readonly<{ a: number }>>
const key13 = useState('13', ref(shallowRef({}))); // ShallowRef<{ a: Ref<number, number> }>
const key14 = useState(
  'I-dare-you-to-go-harder-than-this',
  shallowRef(
    ref(
      reactive(
        shallowReactive(
          shallowReadonly({
            a: shallowReadonly({
              b: ref(1),
              c: reactive({
                d: shallowReactive({
                  e: readonly(ref(2)),
                  f: shallowReadonly({
                      g: ref(3),
                      h: ref(4),
                    }),
                }),
                g: shallowReadonly({
                  h: ref(4),
                  i: shallowReactive({
                    j: readonly(ref(5)),
                    k: shallowReadonly(ref(6)),
                  }),
                }),
              }),
            }),
          }),
        ),
      ),
    ),
  ),
);

type Key14Type =  Ref<Reactive<ShallowReactive<Readonly<{
    a: Readonly<{
        b: Ref<number>;
        c: {
            d: ShallowReactive<{
                e: Readonly<Ref<number>>;
                f: Readonly<{
                    g: Ref<number>;
                    h: Ref<number>;
                }>;
            }>;
            g: {
                ...;
            };
        };
    }>;
}>>>, Reactive<...>>

You can use any of Vue’s reactivity APIs as state, and they’ll remain fully reactive and properly typed, no matter how simple or complex the state structure is. Whether it’s a ref, a plain value, or a deeply nested object, everything works seamlessly. Think of it as a simple way to elevate your reactive variable to a global scope, without compromising on Vue’s core principles.

APIs

| API | Description | |---------------|-----------------------------------------------------------------------------| | useState | Creates a reactive state reference based on the given value and it will be persisted across the app. | | disposeState| Disposes of the state associated with the given key. | | resetStates | Resets all states by removing all key-value pairs. | | getStates | Retrieves a readonly version of all states. | | getState | Retrieves a computed reference to the state associated with the given key. |

License

MIT

Made with ❤️ by Ragab.

🇵🇸Alhamdulillah, Allah is the best of the planners!🇵🇸