前端状态管理渐进迁移
将手写状态逻辑逐步迁入新版工具集,保留状态结构与动作兼容,并验证异步请求及测试。
完整提示词
<context>
Moving to Redux Toolkit is a modernisation inside Redux, not a rewrite, and it can be done one reducer at a time because slices and old reducers live side by side in the same root reducer. What catches people out: `configureStore` turns on immutability and serializability checks in development, which immediately expose existing state mutations and non-serializable values (Dates, Promises, class instances, Maps) that were silently working; decide per case whether to fix the data or configure the check, not disable it wholesale. `createSlice` reducers use Immer: mutate the draft or return a new value, never both. Slice action types become `sliceName/caseName`, so other reducers, sagas, middleware or analytics that match on the old type strings break unless they listen via `extraReducers` or the old strings are kept. In RTK 2 the object form of `extraReducers` is gone (use the builder callback) and `createStore` is deprecated. `createAsyncThunk` dispatches pending, fulfilled and rejected actions and serialises errors. RTK Query replaces data-fetching thunks plus hand-written loading flags and cache, but the same server data must not also live in a slice. Selectors that return new objects cause re-renders; `createSelector` fixes that.
</context>
<task>
Migrate {{scope}} to Redux Toolkit.
{{#notes}}
Notes:
{{notes}}
{{/notes}}
1. Read the store setup, root reducer, middleware, each reducer in scope, its action creators, every place that dispatches or matches its action types (components, sagas, middleware, other reducers, tests), and its selectors.
2. Replace `createStore` with `configureStore`, keeping existing reducers and middleware. Run the app and tests; list every immutability or serializability warning with its cause, and fix mutations rather than turning checks off.
3. Plan the order: leaf reducers with few cross-dependencies first, data-fetching reducers last. Keep the state shape unchanged so selectors and components do not move.
4. Convert one reducer per commit to `createSlice`, export its action creators, update dispatch sites, and handle actions from other slices with the builder form of `extraReducers`. Keep or map old action type strings where sagas, middleware or other code depend on them.
5. Convert thunks to `createAsyncThunk`, or for server data, to an RTK Query API slice with tags for invalidation; then remove the duplicated loading, error and cache state.
6. Run the type check, tests and the app after each step; add reducer tests that dispatch the new action creators where coverage is missing.
</task>
<constraints>
- Do not change the shape of state in the same commit as a slice conversion.
- Do not disable the immutability or serializability middleware globally; scope any exception and explain it.
- Do not keep server data in both a slice and RTK Query.
- Do not rewrite components to hooks as part of this change unless asked.
{{> guardrails/verify-before-done}}
{{> guardrails/scope-discipline}}
</constraints>
<output_format>
## Current store
Reducers, middleware, cross-dependencies on action types, data fetching.
## Migration order
Ordered list with the reason for each position.
## Changes made
Per reducer: slice created, dispatch sites changed, type strings kept or mapped, state removed.
## Verification
Warnings found and fixed, type check, tests and manual checks.
## Next steps
Remaining reducers, RTK Query candidates, selector work.
</output_format>可替换内容
- {{scope}}
- {{#notes}}
- {{notes}}
- {{/notes}}
- {{> guardrails/verify-before-done}}
- {{> guardrails/scope-discipline}}
分类与标签
编程开发、状态管理、渐进迁移、动作兼容、异步请求