anhpht06/frontend-skill-pack/02-store
Claude Code and agent skill by anhpht06/frontend-skill-pack
- community
Checking that the SKILL.md file is still in the repository.
Hướng dẫn sử dụng Zustand cho việc quản lý trạng thái toàn cục (Global State).
Install
npx vskill@latest install anhpht06/frontend-skill-pack/02-storeSKILL.md
The same SKILL.md was published earlier as anhpht06/frontend-skill-pack/02-store.
Global State Management with Zustand
Tài liệu này định nghĩa các quy tắc khi sử dụng Zustand để quản lý trạng thái.
1. Mục đích sử dụng (UI State vs Server State)
- Zustand CHỈ ĐƯỢC DÙNG cho Global UI State (trạng thái giao diện người dùng toàn cục). Ví dụ:
isSidebarOpen,themeMode,activeModal. - TUYỆT ĐỐI KHÔNG dùng Zustand để lưu trữ Server State (dữ liệu trả về từ API). Việc lưu trữ, caching và đồng bộ dữ liệu từ server là trách nhiệm của TanStack Query.
2. Quy tắc Selector (Tránh Re-render)
- Luôn sử dụng pattern Selector khi trích xuất giá trị từ store để tránh component bị re-render không cần thiết khi các state khác thay đổi.
```typescript // ❌ Sai (Component sẽ re-render khi BẤT KỲ state nào trong store thay đổi) const store = useUIStore(); const isOpen = store.isSidebarOpen;
// ✅ Đúng (Component CHỈ re-render khi isSidebarOpen thay đổi) const isSidebarOpen = useUIStore(state => state.isSidebarOpen); ```
3. Quy tắc Persist (Lưu trữ cục bộ)
- Khi sử dụng middleware
persistcủa Zustand, KHÔNG BAO GIỜ sử dụngwindow.localStoragetrực tiếp. - Bắt buộc phải sử dụng
safeLocalStorage(được định nghĩa trongsrc/helpers/storage.helpers.tshoặc tương đương) để tránh các lỗi crash trên trình duyệt (ví dụ: chế độ ẩn danh chặn localStorage).
4. Partialize (Chọn lọc dữ liệu lưu trữ)
- Khi dùng
persist, luôn sử dụng thuộc tínhpartializeđể chỉ lưu những trạng thái thực sự cần thiết. - Không lưu các trạng thái tạm thời như
isLoading,error, hoặc các cờ (flags) liên quan đến trạng thái đang xử lý của một action.
``typescript export const useUIStore = create<UIState>()( persist( (set) => ({ isSidebarOpen: true, isLoading: false, // Trạng thái tạm thời toggleSidebar: () => set((state) => ({ isSidebarOpen: !state.isSidebarOpen })), }), { name: "ui-storage", storage: createJSONStorage(() => safeLocalStorage), // Chỉ lưu isSidebarOpen xuống storage partialize: (state) => ({ isSidebarOpen: state.isSidebarOpen }), } ) ); ``
5. Tổ chức file
- Khai báo các file store trong thư mục
src/store/. - Đặt tên file theo định dạng
[name].store.ts(ví dụ:ui.store.ts,auth.store.ts).
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingvi/02-store/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/anhpht06/frontend-skill-pack/02-store)