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

easy-zustand

v4.1.0

Published

Easy Zustand

Downloads

163

Readme

easy-zustand

English | 简体中文

简介

基于 Zustand

Usage

  1. createStore 传入你的原始状态,类型会被自动识别。

    import createStore from "easy-zustand"
    
    const useStore = createStore({
        name: "Tom",
        age: 18
    })

    有时候,你可能想要确保某些属性不可被修改,比如常量或者方法。只需要先定义类型。

    interface Info {
        name: string
        hobby: string
        readonly age: number
        readonly say: () => void
    }
    
    const useStore = createStore<Info>(
        {
            name: "Tom",
            hobby: "movie",
            age: 18,
            say: () => console.log("Hello, world")
        }
    )

    在上面案例中, ageheight 不会出现 setState 的可修改的属性中

  2. 在函数式组件中使用 useStore

    const [state, setState] = useStore()
  3. 直接读取状态

    <div>{state.name}</div>
  4. 通过 setState 更新状态。这有点像 React 的类式组件,你可以只修改你想要修改的属性。

    <button onClick={() => setState({ name: "Jerry" })}>Add</button>

    同样, 你也可以传入一个以之前状态为参数的函数

    setState(prevState => ({ hobby: prevState.hobby + " music" }))
  5. 持久化存储

    import { createPersistentStore, PersistentStoreOption } from "easy-zustand"
    
    interface StateStorage {
        getItem: (name: string) => string | null | Promise<string | null>;
        setItem: (name: string, value: string) => void | Promise<void>;
        removeItem: (name: string) => void | Promise<void>;
    }
    
    interface PersistentStoreOption {
        /** 用来标识状态的唯一 id */
        name: string
        /** 改变状态是否完全覆盖,默认为 false */
        replace?: boolean
        /** 用来持久化的存储,默认是 localStorage */
        storage?: StateStorage
    }
    
    const useInfo = createPersistentStore({ age: 18 }, "info")
    
    // 上述等价于
    const useInfo = createPersistentStore({ age: 18 }, { name: "info", replace: false, storage: localStorage })
    

使用案例

import { FC } from "react"
import createStore from "easy-zustand"

// 传入你的原始状态
const useStore = createStore({
    count: 0
})

// 它仍然支持 zustand 的三个原生用法

useStore.getState

useStore.setState

useStore.subscribe

// 在组件中使用
const App: FC = () => {
    const [state, setState] = useStore()

    return (<>
        <div>count is {store.count}</div>
        <button onClick={() => setState({ count: store.count + 1 })}>Add</button>
    </>)
}

// 你也可以得到 zustand 创建的原生 store 对象
const originStore = useStore.origin