跳到正文
Skillpedia
网站与界面(前端)40k · #7387

react18-lifecycle-patterns

它能做什么

帮助你把 React 代码中旧的“生命周期”方法更新到新版本的写法。它会告诉你如何把代码移动到正确的位置,使其能与最新的 React 版本良好配合。

适用场景

  • ✓当你的 React 代码中使用了已经不再推荐的生命周期方法时。
  • ✓当你在纠结该用 `getDerivedStateFromProps` 还是 `componentDidUpdate` 时。
  • ✓当你需要添加 `getSnapshotBeforeUpdate` 来处理更新时。

技能链接

https://github.com/github/awesome-copilot/blob/main/skills/react18-lifecycle-patterns/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/github/awesome-copilot/blob/main/skills/react18-lifecycle-patterns/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我该如何迁移 React 组件中用来设置初始状态的 `componentWillMount` 方法?”

每周 5 个新技能,1 分钟读完。

工作原理

本指南会把旧方法的作用与 React 18 中实现相同功能的新方式进行对比。它会根据旧方法的功能,为你指向具体的示例。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

React 18 Lifecycle Patterns

Reference for migrating the three unsafe class component lifecycle methods to React 18.3.1 compliant patterns.

Quick Decision Guide

Before migrating any lifecycle method, identify the semantic category of what the method does. Wrong category = wrong migration. The table below routes you to the correct reference file.

componentWillMount - what does it do?

What it does Correct migration Reference
Sets initial state (this.setState(...)) Move to constructor → componentWillMount.md
Runs a side effect (fetch, subscription, DOM) Move to componentDidMount → componentWillMount.md
Derives initial state from props Move to constructor with props → componentWillMount.md

componentWillReceiveProps - what does it do?

What it does Correct migration Reference
Async side effect triggered by prop change (fetch, cancel) componentDidUpdate → componentWillReceiveProps.md
Pure state derivation from new props (no side effects) getDerivedStateFromProps → componentWillReceiveProps.md

componentWillUpdate - what does it do?

What it does Correct migration Reference
Reads the DOM before update (scroll, size, position) getSnapshotBeforeUpdate → componentWillUpdate.md
Cancels requests / runs effects before update componentDidUpdate with prev comparison → componentWillUpdate.md

The UNSAFE_ Prefix Rule

Never use UNSAFE_componentWillMount, UNSAFE_componentWillReceiveProps, or UNSAFE_componentWillUpdate as a permanent fix.

Prefixing suppresses the React 18.3.1 warning but does NOT:

  • Fix concurrent mode safety issues
  • Prepare the codebase for React 19 (where these are removed, with or without the prefix)
  • Fix the underlying semantic problem the migration is meant to address

The UNSAFE_ prefix is only appropriate as a temporary hold while scheduling the real migration sprint. Mark any UNSAFE_ prefix additions with:

// TODO: React 19 will remove this. Migrate before React 19 upgrade.
// UNSAFE_ prefix added temporarily - replace with componentDidMount / getDerivedStateFromProps / etc.

Reference Files

Read the full reference file for the lifecycle method you are migrating:

  • references/componentWillMount.md - 3 cases with full before/after code
  • references/componentWillReceiveProps.md - getDerivedStateFromProps trap warnings, full examples
  • references/componentWillUpdate.md - getSnapshotBeforeUpdate + componentDidUpdate pairing

Read the relevant file before writing any migration code.

常见问题

什么是“生命周期方法”?

这些是 React 组件中的特殊函数,会在特定时机运行代码,比如组件被创建、更新或移除的时候。

为什么不能直接继续使用旧方法,比如 `UNSAFE_componentWillMount`?

使用 `UNSAFE_` 前缀只是隐藏了警告,并没有解决与新版 React 的兼容性问题。它只是临时的应急办法,并不是长久的解决方案。

在哪里可以找到迁移的代码示例?

详细示例放在单独的参考文件中,比如 `componentWillMount.md`、`componentWillReceiveProps.md` 和 `componentWillUpdate.md`,本指南会指向这些文件。

详情

作者的原始描述

Provides exact before/after migration patterns for the three unsafe class component lifecycle methods - componentWillMount, componentWillReceiveProps, and componentWillUpdate - targeting React 18.3.1. Use this skill whenever a class component needs its lifecycle methods migrated, when deciding between getDerivedStateFromProps vs componentDidUpdate, when adding getSnapshotBeforeUpdate, or when fixi

repo: github/awesome-copilot

热度

GitHub 星标
40k
排名
#7387
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能