Zum Inhalt springen
Skillpedia
Websites & UI (Frontend)40k · #7387

react18-lifecycle-patterns

Was er macht

Hilft dir, alte 'Lifecycle'-Methoden in React-Code auf neuere Versionen umzustellen. Es zeigt dir, wie du Codeteile an die richtigen Stellen verschiebst, damit sie mit aktuellen React-Versionen gut funktionieren.

Wann du ihn nutzt

  • ✓Wenn du alten React-Code mit Lifecycle-Methoden hast, die nicht mehr empfohlen werden.
  • ✓Wenn du entscheiden musst, ob du 'getDerivedStateFromProps' oder 'componentDidUpdate' nutzen sollst.
  • ✓Wenn du 'getSnapshotBeforeUpdate' hinzufügen musst, um Updates zu verarbeiten.

Link zum Skill

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

Kopiere ihn und füge ihn in deine KI ein (Claude, ChatGPT oder Cursor) mit der Bitte, den Anweisungen zu folgen.

So nutzt du ihn

Am einfachsten: Kopiere die Nachricht und füge sie in deine KI ein. Sie liest den Skill und folgt den Anweisungen.

Fertige Nachricht zum Einfügen

Nutze diesen Skill, um mir zu helfen: https://github.com/github/awesome-copilot/blob/main/skills/react18-lifecycle-patterns/SKILL.md Lies die SKILL.md unter diesem Link und folge ihren Anweisungen für meine Aufgabe.
Soll deine KI Skills wie diesen selbst finden?Installiere den kostenlosen Skillpedia-MCP: eine Zeile, und sie durchsucht vor jeder Aufgabe 15.000+ Skills.In 1 Minute installieren →

Beispielanfrage an deine KI

“Wie migriere ich die Methode 'componentWillMount', die in meiner React-Komponente den Anfangszustand setzt?”

5 neue Skills pro Woche. 1 Minute Lesezeit.

So funktioniert es

Dieser Guide vergleicht, was eine alte Methode macht, mit den neuen Wegen, dasselbe in React 18 zu erreichen. Abhängig von der Funktion der alten Methode verweist er dich auf passende Beispiele.

Technischer Inhalt (originale SKILL.md)

Originaltext des Autors, auf Englisch, direkt von 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.

Häufige Fragen

Was sind 'Lifecycle-Methoden'?

Das sind spezielle Funktionen in React-Komponenten, die Code zu bestimmten Zeitpunkten ausführen, zum Beispiel beim Erstellen, Aktualisieren oder Entfernen der Komponente.

Warum kann ich nicht einfach die alten Methoden wie 'UNSAFE_componentWillMount' weiter nutzen?

Das Präfix 'UNSAFE_' blendet nur eine Warnung aus, löst aber keine Kompatibilitätsprobleme mit neueren React-Versionen. Es ist eine Notlösung und keine dauerhafte Lösung.

Wo finde ich die Code-Beispiele zur Migration?

Ausführliche Beispiele stehen in separaten Referenzdateien wie 'componentWillMount.md', 'componentWillReceiveProps.md' und 'componentWillUpdate.md', auf die der Guide verweist.

Details

Originalbeschreibung des Autors

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

Beliebtheit

GitHub-Sterne
40k
Platz im Ranking
#7387
Lizenz
MIT
Aktualisiert
15.10.2026

Ist dieser Skill nützlich?

0

Anmelden um abzustimmen.

Kommentare

  • …

Weitere Skills aus diesem Repository

Ähnliche Skills