react-state-management – skill Lập trình cho agent AI
Làm chủ quản lý state React hiện đại với Redux Toolkit, Zustand, Jotai và React Query. Dùng khi thiết lập global state, quản lý server state, hoặc chọn giải pháp quản lý state phù hợp.
Cài skill react-state-management bằng một lệnh
Chọn công cụ bạn đang dùng, copy lệnh và dán vào terminal. Skill sẽ nằm trong thư mục skills của công cụ đó và sẵn sàng dùng ở phiên làm việc tiếp theo.
Claude Code
mkdir -p ~/.claude/skills && curl -fsSL https://skill.dn1nguoi.com/z/react-state-management-2d045.tgz | tar xz -C ~/.claude/skills
Gọi trong Claude Code: /react-state-management – rồi mô tả công việc bạn muốn làm.
Codex
mkdir -p ~/.codex/skills && curl -fsSL https://skill.dn1nguoi.com/z/react-state-management-2d045.tgz | tar xz -C ~/.codex/skills
Gọi trong Codex: $react-state-management – rồi mô tả công việc bạn muốn làm.
Hermes Agent
mkdir -p ~/.hermes/skills && curl -fsSL https://skill.dn1nguoi.com/z/react-state-management-2d045.tgz | tar xz -C ~/.hermes/skills
Gọi trong Hermes Agent: /react-state-management – rồi mô tả công việc bạn muốn làm.
DeepSeek
mkdir -p ~/.deepcode/skills && curl -fsSL https://skill.dn1nguoi.com/z/react-state-management-2d045.tgz | tar xz -C ~/.deepcode/skills
Gọi trong DeepSeek: /react-state-management – rồi mô tả công việc bạn muốn làm.
Nội dung SKILL.md
Nội dung SKILL.md gốc bằng tiếng Anh.
React State Management
Comprehensive guide to modern React state management patterns, from local component state to global stores and server state synchronization.
When to Use This Skill
- Setting up global state management in a React app
- Choosing between Redux Toolkit, Zustand, or Jotai
- Managing server state with React Query or SWR
- Implementing optimistic updates
- Debugging state-related issues
- Migrating from legacy Redux to modern patterns
Core Concepts
1. State Categories
| Type | Description | Solutions |
| ---------------- | ---------------------------- | ----------------------------- |
| Local State | Component-specific, UI state | useState, useReducer |
| Global State | Shared across components | Redux Toolkit, Zustand, Jotai |
| Server State | Remote data, caching | React Query, SWR, RTK Query |
| URL State | Route parameters, search | React Router, nuqs |
| Form State | Input values, validation | React Hook Form, Formik |
2. Selection Criteria
Small app, simple state → Zustand or Jotai
Large app, complex state → Redux Toolkit
Heavy server interaction → React Query + light client state
Atomic/granular updates → JotaiQuick Start
Zustand (Simplest)
// store/useStore.ts
import { create } from 'zustand'
import { devtools, persist } from 'zustand/middleware'
interface AppState {
user: User | null
theme: 'light' | 'dark'
setUser: (user: User | null) => void
toggleTheme: () => void
}
export const useStore = create<AppState>()(
devtools(
persist(
(set) => ({
user: null,
theme: 'light',
setUser: (user) => set({ user }),
toggleTheme: () => set((state) => ({
theme: state.theme === 'light' ? 'dark' : 'light'
})),
}),
{ name: 'app-storage' }
)
)
)
// Usage in component
function Header() {
const { user, theme, toggleTheme } = useStore()
return (
<header className={theme}>
{user?.name}
<button onClick={toggleTheme}>Toggle Theme</button>
</header>
)
}Detailed patterns and worked examples
Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.
Best Practices
Do's
- Colocate state - Keep state as close to where it's used as possible
- Use selectors - Prevent unnecessary re-renders with selective subscriptions
- Normalize data - Flatten nested structures for easier updates
- Type everything - Full TypeScript coverage prevents runtime errors
- Separate concerns - Server state (React Query) vs client state (Zustand)
Don'ts
- Don't over-globalize - Not everything needs to be in global state
- Don't duplicate server state - Let React Query manage it
- Don't mutate directly - Always use immutable updates
- Don't store derived data - Compute it instead
- Don't mix paradigms - Pick one primary solution per category
Migration Guides
From Legacy Redux to RTK
// Before (legacy Redux)
const ADD_TODO = "ADD_TODO";
const addTodo = (text) => ({ type: ADD_TODO, payload: text });
function todosReducer(state = [], action) {
switch (action.type) {
case ADD_TODO:
return [...state, { text: action.payload, completed: false }];
default:
return state;
}
}
// After (Redux Toolkit)
const todosSlice = createSlice({
name: "todos",
initialState: [],
reducers: {
addTodo: (state, action: PayloadAction<string>) => {
// Immer allows "mutations"
state.push({ text: action.payload, completed: false });
},
},
});Skill liên quan
- pr-lens – Vẽ thay đổi mã thành SVG kiến trúc/luồng dữ liệu có hoạt ảnh.
- community-building – Khi người dùng muốn xây dựng, phát triển hoặc cải thiện cộng đồng lập trình viên trên…
- redteam-mindset – Kỷ luật người vận hành red-team — các điều chỉnh tư duy giúp phân biệt kiểm thử tấn công…
- pyzotero – Quản lý thư viện tham khảo Zotero bằng client Python pyzotero: lấy, tạo, cập nhật và xóa…
- complexity-cuts – Giảm Big-O của mã hiện có theo quy trình từng biến đổi một, với kiểm chứng–hoàn…
- comprehensive-review-full-review – Dùng khi làm việc với comprehensive review full review.
- comprehensive-review-pr-enhance – Tạo mô tả PR có cấu trúc từ diff, thêm danh sách kiểm tra review, đánh giá rủi ro và tóm…
- concise-planning – Dùng khi người dùng yêu cầu kế hoạch cho một tác vụ lập trình, để tạo danh sách kiểm tra…