migrate-state-management
它能做什么
这个技能指导智能体在 React 应用中重构状态管理,从 Redux 和 React Context 迁移出来。适合正在更新状态代码的前端开发者。
适用场景
- ✓当你想移除 Redux 或 React Context,并把数据获取状态迁移到 React Query 时。
- ✓当某个筛选条件或时间范围应保存在 URL 中以便分享,并且你想使用 nuqs 时。
- ✓当全局客户端状态需要一个更简单的存放位置,并且你想把它迁移到 Zustand 时。
技能链接
https://github.com/SigNoz/signoz/blob/main/frontend/.cursor/skills/migrate-state-management/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“能把 Redux 存储中的 API 数据迁移到 React Query,并把筛选条件保留在 URL 中吗?”
每周 5 个新技能,1 分钟读完。
工作原理
智能体将每一部分状态分类为服务器状态、URL 状态或全局客户端状态,然后把每部分迁移到与之匹配的工具中。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
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:
- Find where the data is fetched (existing
useQuery/API call) and where it is dispatched or set in Context/Redux. - Remove the dispatch/set that writes API results into Redux/Context.
- Expose a single hook that uses the query and returns the same shape consumers expect (use
useMemofor derived objects likeconfigsto avoid unnecessary re-renders). - Replace Redux/Context consumption with the new hook. Prefer generated React Query hooks from
frontend/src/api/generatedwhen available. - 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:
- Identify which Redux/Context fields are shareable or already reflected in the URL (e.g.
currentPage,timeRange,selectedFilter). - Add nuqs (or use existing):
useQueryState('param', parseAsString.withDefault('…'))(orparseAsInteger, etc.). - Replace reads/writes of those fields with nuqs hooks. Use typed parsers; avoid ad-hoc
useSearchParamsencoding/decoding. - 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:
- Create one store per domain (e.g.
DashboardStore,QueryBuilderStore). Onecreate()per module; for large state use slice factories and combine. - Put state properties first, then actions. Use
set(orsetState/getState()+set) for updates; never mutate state directly. - Replace Context/Redux consumption with the store hook and a selector so only the used slice triggers re-renders.
- 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):
常见问题
为什么不保留 Redux?
技能说明不要引入或推荐 Redux 和 React Context,并要迁移现有的用法。
nuqs 负责什么?
它负责那些应该可以在 URL 中分享的状态,例如筛选条件、页码和选中项。
本地状态怎么办?
只属于单个组件的状态应使用 useState 或 useReducer。
详情
作者的原始描述
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
热度
- GitHub 星标
- 33k
- 排名
- #9146
- 许可证
- —
- 更新时间
- 2026/11/2
评论
- …