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

zustore

v1.4.17

Published

A lightweight global state management library for React built on contextApi.

Downloads

4,222

Readme

zustore (*Global State Manager*)

A lightweight, flexible global state management library for React applications, built on top of ContextApi. This library provides an intuitive API for managing global state, dispatching actions, and keeping your application state organized.


Table of Contents


Features

  • 🌀 Simple global state management with ContextApi.
  • ⚡ Dynamic action dispatching.
  • 💾 Temporary state support for transient updates.
  • 🔄 Reset and dirty state management.
  • 🚫 Add state without re-rendering: Modify or add new state slices without triggering the updated value until you render it.

Installation

Install the package via NPM or Yarn:

npm install zustore
# or
yarn add zustore

Usage

Setup

create the provider if you want to add a initial state or make a actions

if you don’t need the initial state and make a actions don’t add the StateProvider

"use client"; // don’t forget to add use clint if you are in a next.js

import { ReactNode } from "react";
import { CreateDispatchType, initial } from "zustore";

const initialState = {
  name: "Ibrahim",
  info: {
    age: 22,
  },
};

// Define the createDispatch function
const createDispatch: CreateDispatchType = (name, payload, tools) => {
  const { dispatch, addState, reset } = tools;

  // Action functions
  const setAge = () => {
    const age = payload.value;
    dispatch({ age }, "info"); // Example of using addState
  };

  const lang = () => {
    const { lang } = payload;
    addState({ lang }, "info2"); // Example of using addState
  };

  // Switch based on the function name
  switch (name) {
    case "setAge":
      return setAge();
    case "lang":
      return lang();
    default:
      console.log("No matching action for:", name);
      break;
  }
};

const StateProvider = initial(initialState, createDispatch);

const Root = ({
  children,
}: Readonly<{
  children: ReactNode;
}>) => {
  return <StateProvider>{children}</StateProvider>;
};

export default Root;

Then import the hooks and start managing your global state:

import React from "react";
import { useDispatch, useSelector } from "zustore";

const App = () => {
  const { dispatch, dispatcher, addState } = useDispatch();

  // Accessing state
  const name = useSelector("info.name", "Default Name");
  const age = useSelector("info.age", 0);

  // or can get multiple
  const [name, age] = useSelector(
    ["info.name", "info.age"],
    ["Default Name", 0]
  );

  const updateAge = () => {
    dispatcher("setAge", { value: 22 }); // Dispatch an action to update age
    addState({ job: "front end" }); // this will add a value to state and  not be render until the first dispatch
    // update state
    dispatch({ name: "Ibrahim" }, "info");
    // or
    dispatch({ name: "Ibrahim" });
  };

  return (
    <div>
      <h1>{name}</h1>
      <p>Age: {age}</p>
      <button onClick={updateAge}>Update Age</button>
    </div>
  );
};

export default App;

Available Hooks

useDispatch

Provides access to dispatch and state-management methods.

import { useDispatch } from "zustore";

const { dispatch, dispatcher, reset, dirty, addState } = useDispatch();
Methods:
  • dispatch: Directly update or replace state.
  • dispatcher(action, payload): Dispatch an action using the predefined createDispatch logic.
  • reset(keys): Reset specific state slices to their initial values.
  • dirty(keys): Remove specific keys from the state.
  • addState(newState, key?): Add or update state slices dynamically.

useSelector

Access specific parts of the global state.

import { useSelector } from "zustore";

const value = useSelector("key", "defaultValue");
  • key: The state key to retrieve.
  • defaultValue: Fallback value if the key does not exist.

Global State Example

Customize your initial global state in global-state.ts:

export const globalState = {
  info: {
    name: "John Doe",
    age: 25,
  },
  settings: {
    theme: "light",
    language: "en",
  },
};

Custom Actions

Define actions in createDispatch:

import { CreateDispatch } from "zustore";

const createDispatch = CreateDispatch(({ name, payload, tools }) => {
  const { dispatch, addState, state } = tools;

  const setAge = () => {
    const age = payload.value;
    dispatch({ age }, "info");
  };

  switch (name) {
    /*
      actions => return all actions that you logged
      when you call dispatcher("setAge", { value: 28 })
      (setAge) is the action
    */
    case "setAge":
      return setAge();
    case "lang":
      return lang();
    default:
      console.log("not found");
      break;
  }
});

Dispatch actions using dispatcher:

import { useDispatch } from "zustore";
const { dispatcher } = useDispatch();
dispatcher("setAge", { value: 28 });

Reset the value to the initial value in global state reset:

import { useDispatch } from "zustore";
const { reset } = useDispatch();
reset("key");
// or
reset(["key1", "key2"]);

Dirty the value in the state remove the a value in the state dirty:

import { useDispatch } from "zustore";
const { dirty } = useDispatch();
dirty("key");
// or
dirty(["key1", "key2"]);

License

This project is licensed under the MIT License. See the LICENSE file for details.


Contact

For any inquiries, feedback, or support, feel free to reach out to us through the following channels:

We appreciate your interest in zustore and are happy to assist with any questions or issues you may have.