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

zoov

v0.5.8

Published

A React modular state-management solution, based on Zustand

Downloads

1,310

Readme

Features

  • 😌 Easy: Comfortable type inference
  • ✨ Magic: Update state by just mutate it (with support of immer)
  • 🍳 Tiny: < 200 line code based on Zustand
  • 🧮 Powerful: Modular state management (Redux-like)
  • 📖 Smart: Scope supported with Algebraic Effects
  • 📦 Flexible: Attach state/actions inside or outside React

Quick Start

You can try it on StackBlitz or CodeSandbox

Or install locally

yarn add immer zustand # peer dependencies
yarn add zoov

First Glance

const { use: useCounter } = defineModule({ count: 0 })
  .actions({
    add: (draft) => draft.count++,
    minus: (draft) => draft.count--,
  })
  .computed({
    doubled: (state) => state.count * 2,
  })
  .build();

const App = () => {
  const [{ count }, { add }] = useCounter();
  return <button onClick={add}>{count}</button>;
};

// state is shared
const App2 = () => {
  const [, , { doubled }] = useCounter();
  return <div>doubled: {doubled}</div>;
};

More Examples

Use Methods

import { effect } from 'zoov/effect';

const counterModule = defineModule({ count: 0 })
  .actions({
    add: (draft) => draft.count++,
    minus: (draft) => draft.count--,
  })
  .methods(({ getActions }) => {
    return {
      addAndMinus: () => {
        getActions().add();
        getActions().add();
        setTimeout(() => getActions().minus(), 100);
      },
      // async function is supported
      asyncAdd: async () => {
        await something();
        getActions().add();
      },
      // [TIPS] If you want to `rxjs` in `zoov`, your should first install `rxjs`
      addAfter: effect<number>((payload$) =>
        payload$.pipe(
          exhaustMap((timeout) => {
            return timer(timeout).pipe(tap(() => getActions().add()));
          }),
        ),
      ),
    };
  })
  // using `this` is allowed now! remember to set `noImplicitThis` true in tsconfig
  .methods({
    addTwo() {
      this.getActions().add();
      this.getActions().add();
    },
  })
  .build();

Use Selector

const { use: useCounter } = defineModule({ count: 0, input: 'hello' })
  .actions({
    add: (draft) => draft.count++,
    setInput: (draft, value: string) => (draft.input = value),
  })
  .build();

const App = () => {
  // <App /> will not rerender unless "count" changes
  const [count] = useCounter((state) => state.count);
  return <span>{count}</span>;
};

Additionally, you can install react-tracked and use useTrackedModule to automatically generate selector

// will not rerender unless "count" changes
const [{ count }, { add }] = useTrackedModule(module);

Use subscriptions

const module = defineModule({ pokemonIndex: 0, input: '' })
  .subscribe((state, prevState) => console.log(state)) // subscribe to the whole store
  .subscribe({
    selector: (state) => state.pokemonIndex, // only subscribe to some property
    listener: async (pokemonIndex, prev, { addCleanup }) => {
      const abortController = new AbortController();
      const abortSignal = abortController.signal;
      addCleanup(() => abortController.abort());
      const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${pokemonIndex}`, { signal: abortSignal });
      console.log(await response.json());
    },
  })
  .build();

Use Middleware

// see more examples in https://github.com/pmndrs/zustand/blob/master/src/middleware.ts
const module = defineModule({ count: 0 })
  .actions({ add: (draft) => draft.count++ })
  .middleware((store) => persist(store, { name: 'counter' }))
  .build();

Use internal Actions

// a lite copy of solid-js/store, with strict type check
const { useActions } = defineModule({ count: 0, nested: { checked: boolean } }).build();

const { $setState, $reset } = useActions();

$setState('count', 1);
$setState('nested', 'checked', (v) => !v);
$reset();

Use Provider

import { defineProvider } from 'zoov';

const CustomProvider = defineProvider((handle) => {
  // create a new module scope for all its children(can be nested)
  handle(yourModule, {
    defaultState: {},
  });
  handle(anotherModule, {
    defaultState: {},
  });
});

const App = () => {
  // if a module is not handled by any of its parent, then used global scope
  return (
    <div>
      <CustomProvider>
        <Component />
      </CustomProvider>
      <Component />
    </div>
  );
};

Attach state outside components

// by default, it will get the state under global scope
const actions = module.getActions();
const state = module.getState();

// you can specify the scope with params
const context = useScopeContext();
const scopeActions = module.getActions(context);