返回提示词目录第 1346 页

制定并执行React主版本升级

梳理项目依赖与升级阻碍,按官方指南逐步迁移 React 版本,并通过构建、测试和运行检查验证结果。

完整提示词

<context>
A React major upgrade is rarely blocked by your own code. It is blocked by the libraries that pin `react` and `react-dom` in their peer dependencies, by the type package moving at the same time, and by the test setup. The React team ships a last minor before each major (18.3 before 19) that logs a warning for every API the major removes, so the safe route is: move to that minor, clear its warnings, then take the major. For 18 to 19 the removals that bite are `ReactDOM.render` and `hydrate` (use `createRoot` and `hydrateRoot`), `unmountComponentAtNode`, `findDOMNode`, string refs, legacy context (`contextTypes`, `getChildContext`), `propTypes` checks and `defaultProps` on function components (use default parameters), and `act` moving from `react-dom/test-utils` to `react`. New behaviour to adopt or watch: `ref` is a plain prop on function components so `forwardRef` becomes optional, `<Context>` can be used as the provider, ref callbacks may return a cleanup function (so an implicit return from a ref callback is now an error in TypeScript), and `useRef` needs an argument in the types. Upgrading `@types/react` and `@types/react-dom` with the runtime is part of the job, and the types change on their own (for example the global `JSX` namespace moves under `React.JSX`, and element props default to `unknown` instead of `any`).
</context>

<task>
Upgrade React to {{target_version}}.
{{#project}}
About the project:
{{project}}
{{/project}}

1. Read `package.json`, the lockfile and the entry point. Record the current `react`, `react-dom` and `@types/react*` versions, the framework, and how the root is rendered.
2. Run the build, type check, lint and tests and record the results as the baseline.
3. Get the official React upgrade guide for every major between current and target. Fetch it if you can; otherwise ask the user to paste it and stop. Do not list breaking changes from memory alone.
4. List every dependency with a peer dependency on `react` or `react-dom` (component libraries, form, routing, state, animation, testing libraries). For each, find the first version that supports the target and whether that is itself a major upgrade. Report blockers before touching code.
5. If a last minor before the target exists (for example 18.3 before 19), upgrade to it first, run the app and tests, and collect every deprecation warning.
6. Run the official React codemods for the target, including the types codemod if the project uses TypeScript, then review every change they made. Fix what they cannot: legacy context, string refs, `findDOMNode` uses, `defaultProps` on function components, root rendering.
7. Upgrade `react`, `react-dom` and the type packages together, plus the libraries from step 4. Use the package manager; never force past peer conflicts with flags.
8. Update the test setup (`act` import, testing library versions, any shallow renderer use).
9. Run the baseline checks again, start the app, and click through the main screens with the console open, looking for new warnings and double-rendering effects.
</task>

<constraints>
- Do not use `--force`, `--legacy-peer-deps` or overrides to hide a library that does not support the target. Report it as a blocker with its options (upgrade, replace, wait).
- Do not rewrite components to new patterns (Actions, `use`, compiler-friendly refactors) as part of the upgrade. List them as follow-ups.
- Keep `forwardRef` where it works; removing it is optional cleanup, not required.
{{> guardrails/no-hardcoding-to-pass-tests}}
{{> guardrails/verify-before-done}}
{{> guardrails/scope-discipline}}
</constraints>

<output_format>
## Starting point
Current versions, framework, root rendering, baseline check results.
## Blockers
Table: library, current version, supports target from, action needed. "None" if clear.
## Changes made
Grouped by kind of change (codemod output, manual API replacements, types, tests), with files.
## Verification
Table: check, before, after. Plus what was clicked through in the running app.
## Follow-ups
Deprecations that will break in the next major, optional new patterns, anything not verified.
</output_format>

可替换内容

  • {{target_version}}
  • {{#project}}
  • {{project}}
  • {{/project}}
  • {{> guardrails/no-hardcoding-to-pass-tests}}
  • {{> guardrails/verify-before-done}}
  • {{> guardrails/scope-discipline}}

分类与标签

编程开发、React升级、前端迁移、依赖兼容

前往 AI 图片创作