react18-lifecycle-patterns
Original:🇺🇸 English
Translated
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 fixing React 18 UNSAFE_ lifecycle warnings. Always use this skill before writing any lifecycle migration code - do not guess the pattern from memory, the decision trees here prevent the most common migration mistakes.
28installs
Sourcegithub/awesome-copilot
Added on
NPX Install
npx skill4agent add github/awesome-copilot react18-lifecycle-patternsTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →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 ( | Move to | → componentWillMount.md |
| Runs a side effect (fetch, subscription, DOM) | Move to | → componentWillMount.md |
| Derives initial state from props | Move to | → componentWillMount.md |
componentWillReceiveProps - what does it do?
| What it does | Correct migration | Reference |
|---|---|---|
| Async side effect triggered by prop change (fetch, cancel) | | → componentWillReceiveProps.md |
| Pure state derivation from new props (no side effects) | | → componentWillReceiveProps.md |
componentWillUpdate - what does it do?
| What it does | Correct migration | Reference |
|---|---|---|
| Reads the DOM before update (scroll, size, position) | | → componentWillUpdate.md |
| Cancels requests / runs effects before update | | → componentWillUpdate.md |
The UNSAFE_ Prefix Rule
Never use , , or as a permanent fix.
UNSAFE_componentWillMountUNSAFE_componentWillReceivePropsUNSAFE_componentWillUpdatePrefixing 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:
jsx
// 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:
- - 3 cases with full before/after code
references/componentWillMount.md - - getDerivedStateFromProps trap warnings, full examples
references/componentWillReceiveProps.md - - getSnapshotBeforeUpdate + componentDidUpdate pairing
references/componentWillUpdate.md
Read the relevant file before writing any migration code.