react18-lifecycle-patterns
Skill này làm gì
Giúp cập nhật các phương thức “lifecycle” cũ trong mã React lên phiên bản mới hơn. Nó chỉ cách chuyển các phần mã vào đúng vị trí để hoạt động tốt với các phiên bản React gần đây.
Khi nào nên dùng
- ✓Khi bạn có mã React cũ dùng các phương thức lifecycle không còn được khuyến nghị.
- ✓Khi bạn cân nhắc giữa “getDerivedStateFromProps” và “componentDidUpdate”.
- ✓Khi bạn cần thêm “getSnapshotBeforeUpdate” để xử lý cập nhật.
Liên kết skill
https://github.com/github/awesome-copilot/blob/main/skills/react18-lifecycle-patterns/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Làm thế nào để chuyển phương thức “componentWillMount” đang thiết lập state ban đầu trong component React của tôi?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Hướng dẫn này so sánh việc một phương thức cũ làm gì với các cách mới để làm cùng việc đó trong React 18. Nó chỉ bạn đến các ví dụ cụ thể dựa trên chức năng của phương thức cũ.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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 codereferences/componentWillReceiveProps.md- getDerivedStateFromProps trap warnings, full examplesreferences/componentWillUpdate.md- getSnapshotBeforeUpdate + componentDidUpdate pairing
Read the relevant file before writing any migration code.
Câu hỏi thường gặp
“Lifecycle methods” là gì?
Đây là các hàm đặc biệt trong component React, chạy mã vào những thời điểm nhất định, như khi component được tạo, cập nhật hoặc bị gỡ bỏ.
Vì sao tôi không thể cứ dùng các phương thức cũ như “UNSAFE_componentWillMount”?
Dùng tiền tố “UNSAFE_” chỉ ẩn cảnh báo mà không sửa vấn đề tương thích với các phiên bản React mới. Đây là giải pháp tạm thời, không phải giải pháp lâu dài.
Tôi có thể tìm ví dụ mã chuyển đổi ở đâu?
Các ví dụ chi tiết nằm trong các tệp tham khảo riêng như “componentWillMount.md”, “componentWillReceiveProps.md” và “componentWillUpdate.md”, mà hướng dẫn trỏ đến.
Chi tiết
Mô tả gốc của tác giả
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
Độ phổ biến
- Sao GitHub
- 40k
- Vị trí xếp hạng
- #7387
- Giấy phép
- MIT
- Cập nhật
- 15/10/2026
Bình luận
- …
Thêm skill từ kho mã này
acquire-codebase-knowledge
40kGiúp agent của bạn hiểu và ghi tài liệu cho một dự án mã nguồn có sẵn. Hữu ích khi đón thành viên mới hoặc nắm tổng quan về kiến trúc.
acreadiness-assess
40kGiúp agent của bạn đánh giá mức độ sẵn sàng cho AI của dự án và tạo báo cáo. Hữu ích để biết dự án đã sẵn sàng dùng công cụ AI đến đâu.
acreadiness-generate-instructions
40kGiúp agent của bạn tạo hướng dẫn cho công cụ AI dựa trên kết quả đánh giá. Hữu ích để cải thiện cách AI làm việc với mã và khắc phục các khoảng trống.
acreadiness-policy
40kGiúp agent của bạn xác định hoặc áp dụng chính sách đánh giá dự án bằng AI. Hữu ích để chuẩn hóa cách đánh giá dự án và đảm bảo tuân thủ.
Skill tương tự
angular-developer
280kGiúp agent của bạn viết code Angular và tư vấn cấu trúc dự án, hữu ích khi bắt đầu dự án mới hoặc cải thiện hiệu suất.
frontend-a11y
280kGiúp agent của bạn làm cho giao diện web dễ tiếp cận với mọi người. Đảm bảo website và ứng dụng hoạt động tốt với trình đọc màn hình và điều hướng bằng bàn phím.
frontend-patterns
280kGiúp agent của bạn áp dụng các mẫu hiện đại trong phát triển frontend. Tối ưu hiệu năng, quản lý trạng thái và tuân theo thực hành tốt nhất với React và Next.js.