跳到正文
Skillpedia
网站与界面(前端)48k · #6244

zustand-store-ts

它能做什么

这段代码帮你使用 Zustand 库为网页应用创建数据“store”(状态仓库)。它负责组织数据的保存方式,以及如何访问和修改这些数据,并确保一切与 TypeScript 配合正常工作。

适用场景

  • ✓需要保存许多部分都会用到的信息时。
  • ✓为项目搭建状态管理系统时。
  • ✓组织随时间变化的全局数据时。

技能链接

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/zustand-store-ts/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/zustand-store-ts/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“创建一个用于管理购物车状态的 store,包括商品和总价,并提供添加和删除商品的函数。”

每周 5 个新技能,1 分钟读完。

工作原理

它采用一种模式,分别定义要保存什么(状态)以及如何处理这些数据(操作)。它还能确保数据变化时,只有应用中需要更新的部分会重新渲染。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Zustand Store

Create Zustand stores following established patterns with proper TypeScript types and middleware.

Quick Start

Copy the template from assets/template.ts and replace placeholders:

  • {{StoreName}} → PascalCase store name (e.g., Project)
  • {{description}} → Brief description for JSDoc

Always Use subscribeWithSelector

import { create } from 'zustand';
import { subscribeWithSelector } from 'zustand/middleware';

export const useMyStore = create<MyStore>()(
  subscribeWithSelector((set, get) => ({
    // state and actions
  }))
);

Separate State and Actions

export interface MyState {
  items: Item[];
  isLoading: boolean;
}

export interface MyActions {
  addItem: (item: Item) => void;
  loadItems: () => Promise<void>;
}

export type MyStore = MyState & MyActions;

Use Individual Selectors

// Good - only re-renders when `items` changes
const items = useMyStore((state) => state.items);

// Avoid - re-renders on any state change
const { items, isLoading } = useMyStore();

Subscribe Outside React

useMyStore.subscribe(
  (state) => state.selectedId,
  (selectedId) => console.log('Selected:', selectedId)
);

Integration Steps

  1. Create store in src/frontend/src/store/
  2. Export from src/frontend/src/store/index.ts
  3. Add tests in src/frontend/src/store/*.test.ts

When to Use

This skill is applicable to execute the workflow or actions described in the overview.

Limitations

  • Use this skill only when the task clearly matches the scope described above.
  • Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
  • Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
常见问题

什么是 Zustand?

Zustand 是一个让网页应用中的数据管理变得更简单的库。

什么是 TypeScript?

TypeScript 是一种编程语言,它增加了额外的检查,帮助你避免代码中的错误。

什么是中间件?

中间件就像为 store 添加额外功能的“插件”,比如记录数据变化的日志,或处理异步操作。

详情

作者的原始描述

Create Zustand stores following established patterns with proper TypeScript types and middleware.

repo: sickn33/agentic-awesome-skills

热度

GitHub 星标
48k
排名
#6244
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能