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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@onoxm/zustand-tools

v0.4.4

Published

Library by Zustand + Typescript

Readme

@onoxm/zustand-tools

基于 Zustand 的 TypeScript 工具库,提供灵活的 store hook,支持多种状态选择模式。

安装

npm install @onoxm/zustand-tools
# 或者
yarn add @onoxm/zustand-tools
# 或者
pnpm add @onoxm/zustand-tools

前置依赖

需要在项目中安装 zustand (版本 >= 5):

npm install zustand

API

createStoreHook

将 Zustand 的 useStore 包装为灵活的 hook,支持多种选择模式,并自动使用 shallow 比较优化性能。

签名

function createStoreHook<T extends Record<string, unknown>>(
  useStore: UseBoundStore<StoreApi<T>>
): {
  (): T                                                    // 获取全部状态
  <R>(selector: (state: T) => R): R                        // 选择器函数
  <K extends keyof T>(key: K): T[K]                        // 单个 key
  <K extends keyof T>(keys: K[], include?: true): Pick<T, K> // 多个 keys
  setState: StoreApi<T>['setState']
}

使用示例

import { create } from 'zustand'
import { createStoreHook } from '@onoxm/zustand-tools'

// 1. 定义 store
const useCounterStore = create<{
  count: number
  name: string
  increment: () => void
  decrement: () => void
}>((set) => ({
  count: 0,
  name: 'Counter',
  increment: () => set((state) => ({ count: state.count + 1 })),
  decrement: () => set((state) => ({ count: state.count - 1 })),
}))

// 2. 用 createStoreHook 包装
const useCounter = createStoreHook(useCounterStore)

// 3. 使用
function Counter() {
  // 获取全部状态
  const all = useCounter()

  // 选择器函数
  const double = useCounter((state) => state.count * 2)

  // 单个 key
  const count = useCounter('count')

  // 多个 keys
  const { count, name } = useCounter(['count', 'name'])

  // 使用 setState
  const handleIncrement = () => {
    useCounter.setState((state) => ({ count: state.count + 1 }))
  }

  return (
    <div>
      <div>Count: {count}</div>
      <button onClick={handleIncrement}>+</button>
    </div>
  )
}

特性

  • 🔥 类型安全,完整的 TypeScript 支持
  • 🎯 多种选择模式:全部状态、选择器函数、单个 key、多个 keys
  • ⚡ 自动使用 shallow 比较优化性能
  • 📦 轻量级,无额外依赖

作者

ono

License

MIT