Skillpedia
المواقع والواجهات (frontend)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

انسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.

طريقة الاستخدام

أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.

رسالة جاهزة للصق

استخدم هذه المهارة لمساعدتي: https://github.com/github/awesome-copilot/blob/main/skills/react18-lifecycle-patterns/SKILL.md اقرأ ملف SKILL.md في هذا الرابط ونفّذ تعليماته لمهمتي.
تريد أن يجد الذكاء الاصطناعي مهارات مثل هذه بنفسه؟ثبّت خادم Skillpedia MCP المجاني: سطر واحد، ويبحث في 15,000+ مهارة قبل كل مهمة.ثبّته في دقيقة واحدة →

مثال على ما تطلبه من الذكاء الاصطناعي

“كيف أنقل دالة « componentWillMount » التي تضبط الحالة الأولية في مكون React الخاص بي؟”

5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.

كيف تعمل

يقارن هذا الدليل ما تفعله الدالة القديمة بالطرق الجديدة لتنفيذ الشيء نفسه في 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
آخر تحديث
15‏/10‏/2026

هل هذه المهارة مفيدة؟

0

سجّل الدخول للتصويت.

التعليقات

  • …

المزيد من المهارات من هذا المستودع

مهارات مشابهة