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

zustand-utils

v1.3.2

Published

some utils for zustand

Downloads

165,540

Readme

English | 简体中文

zustand-utils

NPM version NPM downloads install size

Test CI status Deploy CI Coverage

 docs by dumi Build With father

简介

一些 zustand 的工具函数

createContext

zustand v4 中废弃的 createContext 的替代方法。(详情: #1276

import create from 'zustand'
import { createContext } from 'zustand-utils'

const { Provider, useStore } = createContext()

const createStore = () => create(...)

const App = () => (
  <Provider createStore={createStore}>
    ...
  </Provider>
)

const Component = () => {
  const state = useStore()
  const slice = useStore(selector)
  ...

典型用例

createContext

createContext 在组件中的用法

出处: zustand-v3-create-context.md

import create from 'zustand';
import { createContext } from 'zustand-utils';

// 最佳实践:你可以将下面的 createContext() 和 createStore 移动到一个单独的文件(store.js)中,并在需要的地方导入 Provider,useStore。
const { Provider, useStore } = createContext();

const createStore = () =>
  create((set) => ({
    bears: 0,
    increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
    removeAllBears: () => set({ bears: 0 }),
  }));

const Button = () => {
  return (
    // 每次使用 Button 组件都会创建自己的 store 实例,而不是所有组件使用同一个 store。
    <Provider createStore={createStore}>
      <ButtonChild />
    </Provider>
  );
};

const ButtonChild = () => {
  const state = useStore();
  return (
    <div>
      {state.bears}
      <button
        onClick={() => {
          state.increasePopulation();
        }}
      >
        +
      </button>
    </div>
  );
};

export default function App() {
  return (
    <div className="App">
      <Button />
      <Button />
    </div>
  );
}

createContext 的初始化 props 用法

出处: zustand-v3-create-context.md

import create from 'zustand';
import { createContext } from 'zustand-utils';

const { Provider, useStore } = createContext();

export default function App({ initialBears }) {
  return (
    <Provider
      createStore={() =>
        create((set) => ({
          bears: initialBears,
          increase: () => set((state) => ({ bears: state.bears + 1 })),
        }))
      }
    >
      <Button />
    </Provider>
  );
}

使用 createContext 将应用转化为组件

createContext 最常见的用法是将应用程序重构为组件,步骤如下:

  1. 创建一个没有 context 的 App store:
// store.ts
import create from 'zustand';

export const useStore = create((set) => ({
  bears: 0,
  increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
  removeAllBears: () => set({ bears: 0 }),
}));

应用中的组件使用 useStore 来消费 store:

// Component.ts

import { useStore } from './store';

const ButtonChild = () => {
  const state = useStore();
  return (
    <div>
      {state.bears}
      <button
        onClick={() => {
          state.increasePopulation();
        }}
      >
        +
      </button>
    </div>
  );
};
export default ButtonChild;
  1. 只需要将 App 包裹在 createContext 中,不需要重构任何子组件中的代码,就可以将应用转为组件:
// store.ts
import create from "zustand";

+ const createStore = ()=> create((set) => ({
- export const useStore =  create((set) => ({
    bears: 0,
    increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
    removeAllBears: () => set({ bears: 0 })
  }));

+ const { Provider, useStore } = createContext();

+ export { Provider, useStore ,  createStore }
// Wrapper.tsx

import { createStore, Provider } from './store';

const Wrapper = () => {
  return (
    <Provider createStore={createStore}>
      <ButtonChild />
    </Provider>
  );
};

这样它就变成了一个组件,可以被其他任何应用消费。

createStoreUpdater

createStoreUpdater 是一个用于更新 Store 中指定 key 的值的函数。

参数

createStoreUpdater 接收一个 StoreApi 对象作为参数,该对象包含了一些操作 Store 的方法,如 getStatesetStatesubscribedestroy

createStoreUpdater 返回一个函数,该函数接收以下参数:

  • key:需要更新的 Store 中的 key;
  • value:需要更新的值;
  • deps:依赖项数组,默认为 [value]
  • setStoreState:一个可选的回调函数,用于更新 Store 状态,默认为 storeApi.setState

返回值

createStoreUpdater 返回一个函数,该函数用于更新 Store 中指定 key 的值。

示例

import { createStoreUpdater } from 'path/to/createStoreUpdater';
import { useStore } from 'path/to/useStore';

interface User {
  name: string;
  age: number;
}

const storeApi = useStore<User>({ name: '', age: 0 });
const updateUser = createStoreUpdater(storeApi);

// 更新 name
updateUser('name', 'John Doe');

// 更新 age
updateUser('age', 18);

在上面的示例中,我们首先使用 useStore 创建了一个 Store,然后使用 createStoreUpdater 创建了一个更新器 updateUser,最后通过调用 updateUser 来更新 Store 中的 nameage

License

MIT