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

zustify

v1.0.4

Published

A simplified state management wrapper on zustand

Downloads

336

Readme

Introduction

Zustify is a lightweight (~5.5 kb) state management library for React applications, offering a more straightforward and type-safe alternative to Zustand and Redux. If you appreciate the simplicity of Redux APIs but seek a more lightweight and intuitive solution, Zustify is an excellent choice.

Why Zustify?

Zustify combines the best of both worlds from Zustand and Redux. It provides the familiar APIs of Redux for defining actions and managing state but with the simplicity and flexibility of Zustand. With Zustify, you get a type-safe state management solution that is easy to set up, understand, and use in your React projects.

Whether you're a fan of Redux APIs or looking for a more modern approach to state management, Zustify offers a compelling solution. It allows you to define your state and actions with ease, ensuring type safety throughout your application. If you value simplicity, type safety, and the power of Redux-like APIs, Zustify is the state management library for you.

Installation

Install via yarn

yarn add zustify

Install via npm

npm install zustify

Usage Typescript

Create your store

import {createSharedStore, Action} from 'zustify' 

const [useStore, useDispatch] =  createSharedStore(
    // This is your state
    {
        count: 0,
        firstName: 'test',
        lastName: 'test lastname',
    },
    // This is your reducer where you can provide actions
    {
        increment(state, action: Action<number>) {
            const number = action.payload;
            //update the state directly zustify will take care of the rerendering 
            state.count += number;
        },
        decrement(state, action: Action<number>) {
            state.count -= action.payload || 1;
        },
    },
)

The createSharedStore function returns an array with two hooks:

useStore: Allows reading the state by passing a selector.

const count = useStore(state=>state.count)

useDispatch: Provides functions to update or reset the state. actions contains all defined reducers.

const {dispatch,reset,actions}  = useDispatch()

reset:

The reset function provided by useDispatch allows you to reset the state to its initial values. It's particularly useful when you need to revert the state to its original state or clear out any modifications made during runtime.

dispatch:

The dispatch function provided by useDispatch is used to trigger actions defined in the reducer. It takes the action reducer name as its first argument, followed by any payload required by that action. This function is central to updating the state based on predefined actions. Importantly, dispatch ensures type safety by only allowing actions that are explicitly defined in the reducer to be dispatched.

actions:

In Zustify, the action object provided by useDispatch serves as a convenient reference to all the reducer method names. It's essentially an object containing references to each action defined in the reducer. This allows for easy access to the actions within your components without the need to manually reference each one. By providing a centralized object with all action names, Zustify enhances code readability and maintainability. Additionally, it ensures type safety by restricting dispatch calls to only those actions listed within the action object, preventing accidental typos or misuse.

Example Usage


function Mycomponent() {
  const count = useStore((state) => state.count);
  const { dispatch, actions, reset } = useDispatch();

  return (
    <div>
      <div>{count}</div>
      <button onClick={() => { dispatch(actions.increment, 1) }}>Increment</button>
      <button onClick={() => { dispatch(actions.decrement, 1) }}>Decrement</button>
    </div>
  );
}

For using state in a non-reactive way:

const completeState = useCountStore.getState();

Working example:

CodeSandBox Example