Index of every page on this site: llms.txt
All skills
github avatar

/react18-lifecycle-patterns

@f10fdb8 official
by githubgithub/awesome-copilot42k stars
5,426
Run it yourself

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.

Use this Skill: https://skilld.dev/gh/github/awesome-copilot/react18-lifecycle-patterns

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencescomponentWillUpdate.md

≈1.2k tokens on demand. Your agent reads this file only when SKILL.md points to it.

componentWillUpdate Migration Reference

The Core Decision

Does componentWillUpdate read the DOM (scroll, size, position, selection)?
  YES → getSnapshotBeforeUpdate (paired with componentDidUpdate)
  NO (side effects, request cancellation, etc.) → componentDidUpdate

Case A - Reads DOM Before Re-render {#case-a}

The method captures a DOM measurement (scroll position, element size, cursor position) before React applies the next update, so it can be restored or adjusted after.

Before:

class MessageList extends React.Component {
  componentWillUpdate(nextProps) {
    if (nextProps.messages.length > this.props.messages.length) {
      this.savedScrollHeight = this.listRef.current.scrollHeight;
      this.savedScrollTop = this.listRef.current.scrollTop;
    }
  }

  componentDidUpdate(prevProps) {
    if (prevProps.messages.length < this.props.messages.length) {
      const scrollDelta = this.listRef.current.scrollHeight - this.savedScrollHeight;
      this.listRef.current.scrollTop = this.savedScrollTop + scrollDelta;
    }
  }
}

After - getSnapshotBeforeUpdate + componentDidUpdate:

class MessageList extends React.Component {
  // Called right before DOM updates are applied - perfect timing to read DOM
  getSnapshotBeforeUpdate(prevProps, prevState) {
    if (prevProps.messages.length < this.props.messages.length) {
      return {
        scrollHeight: this.listRef.current.scrollHeight,
        scrollTop: this.listRef.current.scrollTop,
      };
    }
    return null; // Return null when snapshot is not needed
  }

  // Receives the snapshot as the third argument
  componentDidUpdate(prevProps, prevState, snapshot) {
    if (snapshot !== null) {
      const scrollDelta = this.listRef.current.scrollHeight - snapshot.scrollHeight;
      this.listRef.current.scrollTop = snapshot.scrollTop + scrollDelta;
    }
  }
}

Why this is better than componentWillUpdate: In React 18 concurrent mode, there can be a gap between when componentWillUpdate runs and when the DOM actually updates. DOM reads in componentWillUpdate may be stale. getSnapshotBeforeUpdate runs synchronously right before the DOM is committed - the reads are always accurate.

The contract:

  • Return a value from getSnapshotBeforeUpdate → that value becomes snapshot in componentDidUpdate
  • Return null → snapshot in componentDidUpdate is null
  • Always check if (snapshot !== null) in componentDidUpdate
  • getSnapshotBeforeUpdate MUST be paired with componentDidUpdate

Case B - Side Effects Before Update {#case-b}

The method cancels an in-flight request, clears a timer, or runs some preparatory side effect when props or state are about to change.

Before:

class SearchResults extends React.Component {
  componentWillUpdate(nextProps) {
    if (nextProps.query !== this.props.query) {
      this.currentRequest?.cancel();
      this.setState({ loading: true, results: [] });
    }
  }
}

After - move to componentDidUpdate (run AFTER the update):

class SearchResults extends React.Component {
  componentDidUpdate(prevProps) {
    if (prevProps.query !== this.props.query) {
      // Cancel the stale request
      this.currentRequest?.cancel();
      // Start the new request for the updated query
      this.setState({ loading: true, results: [] });
      this.currentRequest = searchAPI(this.props.query)
        .then(results => this.setState({ results, loading: false }));
    }
  }
}

Note: The side effect now runs AFTER the render, not before. In most cases this is correct - you want to react to the state that's actually showing, not the state that was showing. If you truly need to run something synchronously BEFORE a render, reconsider the design - that usually indicates state that should be managed differently.


Both Cases in One Component

If a component had both DOM-reading AND side effects in componentWillUpdate:

// Before: does both
componentWillUpdate(nextProps) {
  // DOM read
  if (isExpanding(nextProps)) {
    this.savedHeight = this.ref.current.offsetHeight;
  }
  // Side effect
  if (nextProps.query !== this.props.query) {
    this.request?.cancel();
  }
}

After: split into both patterns:

// DOM read → getSnapshotBeforeUpdate
getSnapshotBeforeUpdate(prevProps, prevState) {
  if (isExpanding(this.props)) {
    return { height: this.ref.current.offsetHeight };
  }
  return null;
}

// Side effect → componentDidUpdate
componentDidUpdate(prevProps, prevState, snapshot) {
  // Handle snapshot if present
  if (snapshot !== null) { /* ... */ }

  // Handle side effect
  if (prevProps.query !== this.props.query) {
    this.request?.cancel();
    this.startNewRequest();
  }
}

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts2mo4 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    The skill provides safe documentation and code examples for migrating React lifecycle methods. No security issues were detected.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: LOW · No issues

  • ZeroLeaks7mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at f10fdb8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 days ago.

Activeupdated 7 months ago
  • React
  • react18
  • lifecycle
  • class-components
  • migration
  • componentwillmount
  • componentwillreceiveprops
  • componentwillupdate
  • getderivedstatefromprops
  • getsnapshotbeforeupdate

README badge

README badge for github/awesome-copilot/react18-lifecycle-patterns

Provides decision trees and before/after code patterns for migrating React class component lifecycle methods (componentWillMount, componentWillReceiveProps, componentWillUpdate) to React 18.3.1 compliant equivalents. Targets the three unsafe lifecycle methods specifically and routes each to the correct replacement (constructor, componentDidMount, getDerivedStateFromProps, getSnapshotBeforeUpdate, or componentDidUpdate) based on whether the code performs side effects or derives state.

Generated from the current SKILL.md.

Does this skill cover all three unsafe lifecycle methods?
Yes. It provides migration patterns for componentWillMount, componentWillReceiveProps, and componentWillUpdate, with decision trees to route each to the correct replacement (constructor, componentDidMount, getDerivedStateFromProps, getSnapshotBeforeUpdate, or componentDidUpdate).
Can I just add the UNSAFE_ prefix to suppress warnings?
No. The skill explicitly warns that UNSAFE_ prefixes suppress warnings but do not fix concurrent mode safety issues or prepare code for React 19, where these methods are removed entirely. Real migration is required.
How do I know which migration pattern to use?
The skill provides a semantic decision guide—you identify what the lifecycle method does (e.g. sets initial state, runs async side effects, reads the DOM) and the table routes you to the correct replacement and reference file.
Does this target a specific React version?
Yes, React 18.3.1. The patterns account for concurrent mode and React 18 specific behavior.

Generated from the current SKILL.md. These answers refresh after source changes.