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.

referencescomponentWillMount.md

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

componentWillMount Migration Reference

Case A - Initializes State {#case-a}

The method only calls this.setState() with static or computed values that do not depend on async operations.

Before:

class UserList extends React.Component {
  componentWillMount() {
    this.setState({ items: [], loading: false, page: 1 });
  }
  render() { ... }
}

After - move to constructor:

class UserList extends React.Component {
  constructor(props) {
    super(props);
    this.state = { items: [], loading: false, page: 1 };
  }
  render() { ... }
}

If constructor already exists, merge the state:

class UserList extends React.Component {
  constructor(props) {
    super(props);
    // Existing state merged with componentWillMount state:
    this.state = {
      ...this.existingState,  // whatever was already here
      items: [],
      loading: false,
      page: 1,
    };
  }
}

Case B - Runs a Side Effect {#case-b}

The method fetches data, sets up subscriptions, interacts with external APIs, or touches the DOM.

Before:

class UserDashboard extends React.Component {
  componentWillMount() {
    this.subscription = this.props.eventBus.subscribe(this.handleEvent);
    fetch(`/api/users/${this.props.userId}`)
      .then(r => r.json())
      .then(user => this.setState({ user, loading: false }));
    this.setState({ loading: true });
  }
}

After - move to componentDidMount:

class UserDashboard extends React.Component {
  constructor(props) {
    super(props);
    this.state = { loading: true, user: null }; // initial state here
  }

  componentDidMount() {
    // All side effects move here - runs after first render
    this.subscription = this.props.eventBus.subscribe(this.handleEvent);
    fetch(`/api/users/${this.props.userId}`)
      .then(r => r.json())
      .then(user => this.setState({ user, loading: false }));
  }

  componentWillUnmount() {
    // Always pair subscriptions with cleanup
    this.subscription?.unsubscribe();
  }
}

Why this is safe: In React 18 concurrent mode, componentWillMount can be called multiple times before mounting. Side effects inside it can fire multiple times. componentDidMount is guaranteed to fire exactly once after mount.


Case C - Derives Initial State from Props {#case-c}

The method reads this.props to compute an initial state value.

Before:

class PriceDisplay extends React.Component {
  componentWillMount() {
    this.setState({
      formattedPrice: `$${this.props.price.toFixed(2)}`,
      isDiscount: this.props.price < this.props.originalPrice,
    });
  }
}

After - constructor with props:

class PriceDisplay extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      formattedPrice: `$${props.price.toFixed(2)}`,
      isDiscount: props.price < props.originalPrice,
    };
  }
}

Note: If this initial state needs to UPDATE when props change later, that's a getDerivedStateFromProps case - see componentWillReceiveProps.md Case B.


Multiple Patterns in One Method

If a single componentWillMount does both state init AND side effects:

// Mixed - state init + fetch
componentWillMount() {
  this.setState({ loading: true, items: [] });              // Case A
  fetch('/api/items').then(r => r.json())                   // Case B
    .then(items => this.setState({ items, loading: false }));
}

Split them:

constructor(props) {
  super(props);
  this.state = { loading: true, items: [] }; // Case A → constructor
}

componentDidMount() {
  fetch('/api/items').then(r => r.json())    // Case B → componentDidMount
    .then(items => this.setState({ items, loading: false }));
}

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.