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

zustand-vault

v1.1.0

Published

zustand-vault is a helper library built on top of zustand for creating and accessing multiple stores in an application.

Downloads

3

Readme

zustand-vault

zustand-vault is a helper library built on top of zustand for creating and accessing multiple stores in an application.

It provides a central access point for all the stores and simplifies the state management process.

The library is designed to work with TypeScript and provides a clean API for accessing and managing state.

Installation

To use 'zustand-vault', you need to install it first. You can install it via npm or yarn. Here are the commands for both:

npm install zustand zustand-vault

yarn add zustand zustand-vault

Getting started

To use 'zustand-vault', you need to create a store first. You can create a store by using the storeBuilder function.

Here's an example:

import { storeBuilder } from 'zustand-vault';

type VaultStore = {
  counter: {
    counter: number;
    increment(): void;
    decrement(): void;
  };
};

const store = storeBuilder<VaultStore>()
  .set('counter', ({ set }) => ({
    counter: 0,
    increment: () => set((state) => ({ counter: state.counter + 1 })),
    decrement: () => set((state) => ({ counter: state.counter - 1 })),
  }))
  .get();

In this example, we create a store with a counter property that has an initial value of 0 and two methods to increment and decrement the counter.

To use the store, you can create a Vault instance by using the createVault function and pass the store to it.

Here's an example:

import { createVault } from 'zustand-vault';

const vault = createVault(store);

Now, you can use the useStore method of the Vault instance to get the state and methods of the store.

Here's an example:

import { createVault } from 'zustand-vault';

const vault = createVault(store);

function MyComponent() {
  const { counter, increment, decrement } = vault.useStore('counter');

  return (
    <div>
      <p>Counter: {counter}</p>
      <button onClick={increment}>Increment</button>
      <button onClick={decrement}>Decrement</button>
    </div>
  );
}

Example code

Here's an example code that uses 'zustand-vault' to create a store with multiple properties:

import { createVault, storeBuilder } from 'zustand-vault';

type VaultStore = {
  counter: {
    counter: number;
    increment(): void;
    decrement(): void;
  };
  toast: {
    visible: boolean;
    show(): void;
    hide(): void;
    toggle(): void;
  };
};

const store = storeBuilder<VaultStore>()
  .set('counter', ({ set }) => ({
    counter: 0,
    increment: () => set((state) => ({ counter: state.counter + 1 })),
    decrement: () => set((state) => ({ counter: state.counter - 1 })),
  }))
  .set('toast', ({ set }) => ({
    visible: false,
    show: () => set({ visible: true }),
    hide: () => set({ visible: false }),
    toggle: () => set((state) => ({ visible: !state.visible })),
  }))
  .get();

const vault = createVault(store);

function MyComponent() {
  const { counter, increment, decrement } = vault.useStore('counter');
  const { visible, toggle } = vault.useStore('toast');

  useEffect(() => {
    console.log('getState():', vault.store('toast').getState().visible);

    return vault.store('toast').subscribe((state) => {
      console.log('state changed:', state.visible);
    });
  }, []);

  return (
    <div>
      <p>Counter: {counter}</p>
      <button onClick={increment}>Increment</button>
      <button onClick={decrement}>Decrement</button>

      <p>Toast: {visible ? 'Visible' : 'Hidden'}</p>
      <button onClick={toggle}>Toggle</button>
    </div>
  );
}