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

toy-zustand

v0.0.5

Published

🐻 Bear necessities for state management in React or Vue

Downloads

3

Readme

NPM version

npm install toy-zustand # or yarn add toy-zustand or pnpm add toy-zustand

vue3版demo:https://codesandbox.io/p/sandbox/vue3-zustand-demo-m63cx9

react版demo:https://codesandbox.io/p/sandbox/react-zustand-demo-n83w33

toy-zustand支持哪些内容?

使用的用法和zustand是一模一样的,这里介绍下常用的使用方法,了解更多的话可以去zustand的官网。 https://zustand-demo.pmnd.rs/


// vue
import { create } from 'zustand/vue'

// react
import { create } from 'zustand/react'

// 中间件immer
import { immer } from "toy-zustand/middleware/immer";

// shallow方法
import shallow from 'toy-zustand/shallow'

创建一个store

我们以vue3为例,创建一个全局store数据。只需要调用create方法,并设置初始化数据即可以,store存储的数据可以是基本数据类型(string number boolean null undefined), 也可以是object 和 function。我们可以function通过set函数来合并我们的store。

import create from "toy-zustand/vue";

const useGlobalStore = create((set, get) => ({
  bears: 10086,
  count: 100,
  increase: (by) => set((state) => ({ bears: (state.bears || 0) + by })),
  reset: () =>
    set({
      count: 0,
      bears: 0,
    }),
  radomCount: () => set(() => ({ count: Math.random() })),
}));

export default useGlobalStore;

store数据的使用

我们可以使用数据在任何地方使用。比如react的当当初一个hooks使用,vue3中通过setUp直接将store暴露给组件。

<!-- Component.vue -->
<script setup>
import useGlobalStore from "../Store/useGlobalStore";

const globalStore = useGlobalStore();
const { bears, count, increase, radomCount } = globalStore;
</script>

<template>
  <h3>Parent</h3>
  <div>
    bears ( {{ bears }} ):
    <button @click="() => increase(1)">增加</button>
  </div>
  <div>
    count ( {{ count }} ): <button @click="() => radomCount()">随机</button>
  </div>
</template>
<!-- Component.react -->
import useGlobalStore from "../Store/useGlobalStore";

const Child = () => {
  const { reset, destroy } = useGlobalStore((state) => ({
    reset: state.reset,
    destroy: state.destroy,
  }));
  return (
    <div>
      <h3>Child</h3>
      <button onClick={() => reset()}>清空</button>
      <button onClick={() => destroy()}>销毁</button>
    </div>
  );
};

export default Child;

直接在js中使用

import useGlobalStore from "../Store/useGlobalStore";

export const getGlobalStore=()=>{
  return useGlobalStore.getState()
}

set的第二个参数,replace

初始话set可以传递第二个参数为boolean类型,为true的会就会替换整个store而不是默认的合并了!

image.png

中间件

image.png

zustand提供很多的中间件,如persist,devtools等 大家想要了解的话,可以去这里看!https://github.com/pmndrs/zustand/tree/main/src/middleware


如果有好玩的库欢迎私聊我,一起学习讨论!