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

@udecode/zustood

v2.0.0

Published

A modular store factory using zustand

Downloads

107,673

Readme

Zustood

Zustand is a small, fast and scalable state-management solution battle-tested against common pitfalls, like the dreaded zombie child problem, react concurrency, and context loss between mixed renderers. It may be the one state-manager in the React space that gets all of these right.

As zustand is un-opinionated by design, it's challenging to find out the best patterns to use when creating stores, often leading to boilerplate code.

Zustood, built on top of zustand, is providing a powerful store factory which solves these challenges, so you can focus on your app.

yarn add zustand @udecode/zustood

Visit zustood.udecode.io for the API.

Why zustood over zustand?

  • Much less boilerplate
  • Modular state management:
    • Derived selectors
    • Derived actions
  • immer, devtools and persist middlewares
  • Full typescript support

Create a store

import { createStore } from '@udecode/zustood'

const repoStore = createStore('repo')({
  name: 'zustood',
  stars: 0,
})
  • the parameter of the first function is the name of the store, this is helpful when you have multiple stores
  • the parameter of the second function is the initial state of your store
  • the main difference with zustand is that you don't need to define a getter and a setter for each field, these are generated by zustood

Note that the zustand store is accessible through:

// hook store
repoStore.useStore

// vanilla store
repoStore.store

Selectors

Hooks

Use the hooks in React components, no providers needed. Select your state and the component will re-render on changes. Use the use method:

repoStore.use.name()
repoStore.use.stars()

We recommend using the global hooks (see below) to support ESLint hook linting.

Getters

Don't overuse hooks. If you don't need to subscribe to the state, use instead the get method:

repoStore.get.name()
repoStore.get.stars()

You can also get the whole state:

repoStore.get.state()

Extend selectors

You generally want to write derived selectors (those depending on other selectors) for reusability. Zustood supports extending selectors with full typescript support:

const repoStore = createStore('repo')({
  name: 'zustood ',
  stars: 0,
})
  .extendSelectors((set, get, api) => ({
    validName: () => get.name().trim(),
    // other selectors
  }))
  .extendSelectors((set, get, api) => ({
    // get.validName is accessible
    title: (prefix: string) =>
      `${prefix + get.validName()} with ${get.stars()} stars`,
  }))
  // extend again...

Actions

Update your store from anywhere by using the set method:

repoStore.set.name('new name')
repoStore.set.stars(repoStore.get.stars + 1)

Extend actions

You can update the whole state from your app:

store.set.state((draft) => {
  draft.name = 'test';
  draft.stars = 1;
});

However, you generally want to create derived actions for reusability. Zustood supports extending actions with full typescript support:

const repoStore = createStore('repo')({
  name: 'zustood',
  stars: 0,
})
  .extendActions((set, get, api) => ({
    validName: (name: string) => {
      set.name(name.trim());
    },
    // other actions
  }))
  .extendActions((set, get, api) => ({
    reset: (name: string) => {
      // set.validName is accessible
      set.validName(name);
      set.stars(0);
    },
  }))
  // extend again...

Global store

After having created many stores, it can be difficult to remember which one to import. By combining all the stores, selectors and actions, just pick what you need using TS autocomplete.

import { mapValuesKey } from '@udecode/zustood';

// Global store
export const rootStore = {
  auth: authStore,
  combobox: comboboxStore,
  contextMenu: contextMenuStore,
  editor: editorStore,
  modal: modalStore,
  repo: repoStore,
  toolbar: toolbarStore,
};

// Global hook selectors
export const useStore = () => mapValuesKey('use', rootStore);

// Global getter selectors
export const store = mapValuesKey('get', rootStore);

// Global actions
export const actions = mapValuesKey('set', rootStore);

Global hook selectors

useStore().repo.name()
useStore().modal.isOpen()

By using useStore(), ESLint will correctly lint hook errors.

Global getter selectors

store.repo.name()
store.modal.isOpen()

These can be used anywhere.

Global actions

actions.repo.stars(store.repo.stars + 1)
actions.modal.open()

These can be used anywhere.

Options

The second parameter of createStore is for options:

export interface CreateStoreOptions<T extends State> {
  middlewares?: any[];
  devtools?: DevtoolsOptions;
  immer?: ImmerOptions;
  persist?: PersistOptions;
}

Middlewares

Zustood is using these middlewares:

  • immer: required. Autofreeze can be enabled using immer.enabledAutoFreeze option.
  • devtools: enabled if devtools.enabled option is true.
  • persist: enabled if persist.enabled option is true. persist implements PersistOptions interface from zustand
  • custom middlewares can be added using middlewares option

Contributing and project organization

Ideas and discussions

Discussions is the best place for bringing opinions and contributions. Letting us know if we're going in the right or wrong direction is great feedback and will be much appreciated!

Become a Sponsor!

Contributors

🌟 Stars and 📥 Pull requests are welcome! Don't hesitate to share your feedback here. Read our contributing guide to get started.

License

MIT