Chuyển đến nội dung
Skillpedia
Website và giao diện (frontend)33k · #9146

migrate-state-management

Skill này làm gì

Skill này hướng dẫn agent AI tái cấu trúc quản lý trạng thái trong ứng dụng React, chuyển khỏi Redux và React Context. Skill dành cho lập trình viên front-end muốn hiện đại hóa mã quản lý trạng thái.

Khi nào nên dùng

  • ✓Khi bạn muốn bỏ Redux hoặc React Context và chuyển trạng thái lấy dữ liệu sang React Query.
  • ✓Khi một bộ lọc hoặc khoảng thời gian nên nằm trên URL để có thể chia sẻ, và bạn muốn dùng nuqs.
  • ✓Khi trạng thái client toàn cục cần một nơi lưu gọn hơn, và bạn muốn chuyển nó sang Zustand.

Liên kết skill

https://github.com/SigNoz/signoz/blob/main/frontend/.cursor/skills/migrate-state-management/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/SigNoz/signoz/blob/main/frontend/.cursor/skills/migrate-state-management/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Bạn có thể chuyển dữ liệu API trong store Redux của chúng tôi sang React Query và giữ các bộ lọc trên URL không?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Agent phân loại từng trạng thái thành trạng thái máy chủ, trạng thái URL hoặc trạng thái client toàn cục, rồi chuyển mỗi loại sang công cụ tương ứng.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Migrate State: Redux/Context → React Query, nuqs, Zustand

Do not introduce or recommend Redux or React Context. Migrate existing usage to the stack below.

1. Classify the state

Before changing code, classify what the state represents:

If the state is… Migrate to Do not use
From API / server (versions, configs, fetched lists, time-series) React Query Redux, Context
Shareable via URL (filters, time range, page, selected ids) nuqs Redux, Context
Global/client UI (dashboard lock, query builder, feature flags, large client objects) Zustand Redux, Context
Local to one component (inputs, toggles, hover) useState / useReducer Zustand, Redux, Context

If one slice mixes concerns (e.g. Redux has both API data and pagination), split: API → React Query, pagination → nuqs, rest → Zustand or local state.

2. Migrate to React Query (server state)

When: State comes from or mirrors an API response (e.g. currentVersion, latestVersion, configs, lists).

Steps:

  1. Find where the data is fetched (existing useQuery/API call) and where it is dispatched or set in Context/Redux.
  2. Remove the dispatch/set that writes API results into Redux/Context.
  3. Expose a single hook that uses the query and returns the same shape consumers expect (use useMemo for derived objects like configs to avoid unnecessary re-renders).
  4. Replace Redux/Context consumption with the new hook. Prefer generated React Query hooks from frontend/src/api/generated when available.
  5. Configure cache/refetch (e.g. refetchOnMount: false, staleTime) so behavior matches previous “single source” expectations.

Before (Redux mirroring React Query):

if (getUserLatestVersionResponse.isFetched && getUserLatestVersionResponse.isSuccess && getUserLatestVersionResponse.data?.payload) {
  dispatch({ type: UPDATE_LATEST_VERSION, payload: { latestVersion: getUserLatestVersionResponse.data.payload.tag_name } })
}

After (single source in React Query):

export function useAppStateHook() {
  const { data, isError } = useQuery(...)
  const memoizedConfigs = useMemo(() => ({ ... }), [data?.configs])
  return {
    latestVersion: data?.payload?.tag_name,
    configs: memoizedConfigs,
    isError,
  }
}

Consumers use useAppStateHook() instead of useSelector or Context. Do not copy React Query result into Redux or Context.

3. Migrate to nuqs (URL / shareable state)

When: State should be in the URL: filters, time range, pagination, selected values, view state. Keep payload small (e.g. Chrome ~2k chars); no large datasets or sensitive data.

Steps:

  1. Identify which Redux/Context fields are shareable or already reflected in the URL (e.g. currentPage, timeRange, selectedFilter).
  2. Add nuqs (or use existing): useQueryState('param', parseAsString.withDefault('…')) (or parseAsInteger, etc.).
  3. Replace reads/writes of those fields with nuqs hooks. Use typed parsers; avoid ad-hoc useSearchParams encoding/decoding.
  4. Remove the same fields from Redux/Context and their reducers/providers.

Before (Context/Redux):

const { timeRange } = useContext(SomeContext)
const [page, setPage] = useDispatch(...)

After (nuqs):

const [timeRange, setTimeRange] = useQueryState('timeRange', parseAsString.withDefault('1h'))
const [page, setPage] = useQueryState('page', parseAsInteger.withDefault(1))

4. Migrate to Zustand (global client state)

When: State is global or cross-component client state: feature flags, dashboard state, query builder state, complex/large client objects (e.g. up to ~1.5–2MB). Not for server cache or local-only UI.

Steps:

  1. Create one store per domain (e.g. DashboardStore, QueryBuilderStore). One create() per module; for large state use slice factories and combine.
  2. Put state properties first, then actions. Use set (or setState / getState() + set) for updates; never mutate state directly.
  3. Replace Context/Redux consumption with the store hook and a selector so only the used slice triggers re-renders.
  4. Remove the old Context provider / Redux slice and related dispatches.

Selector (required):

const isLocked = useDashboardStore(state => state.isDashboardLocked)

Never use useStore() with no selector. Never do state.foo = x inside actions; use set(state => ({ ... })).

Before (Context/Redux):


Câu hỏi thường gặp

Vì sao không giữ Redux?

Skill cho biết không nên giới thiệu hay khuyến nghị Redux hay React Context, và cần chuyển các chỗ đang dùng.

nuqs xử lý những gì?

Nó xử lý trạng thái cần chia sẻ được trên URL, như bộ lọc, trang và mục đã chọn.

Còn trạng thái cục bộ thì sao?

Trạng thái thuộc về một component nên dùng useState hoặc useReducer.

Chi tiết

Mô tả gốc của tác giả

Migrate Redux or React Context to the correct state option (React Query for server state, nuqs for URL/shareable state, Zustand for global client state). Use when refactoring away from Redux/Context, moving state to the right store, or when the user asks to migrate state management.

repo: SigNoz/signoz

Độ phổ biến

Sao GitHub
33k
Vị trí xếp hạng
#9146
Giấy phép
—
Cập nhật
2/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự