JavaScript logoJavaScript/
JS-0456

Avoid using unsafe lifecycle methodsJS-0456

Major severityMajor
Anti-pattern categoryAnti-pattern
react

Specific legacy lifecycle methods are unsafe for use in async React applications and cause warnings in strict mode.

React 16.x introduced replacements for the following legacy methods:

  • componentWillMount
  • componentWillReceiveProps
  • componentWillUpdate

These methods do the same thing as their newer counterparts (prefixed with UNSAFE_). The only difference is that these methods will not exist in future versions of react, i.e. from React 17 onwards. It is recommended to migrate to their aliases:

  • UNSAFE_componentWillMount
  • UNSAFE_componentWillReceiveProps
  • UNSAFE_componentWillUpdate

Bad Practice

class Card extends React.Component {
  componentWillMount() {}
  componentWillReceiveProps() {}
  componentWillUpdate() {}
}
class Card extends SomeOtherBaseClass {
  UNSAFE_componentWillMount() {}
  UNSAFE_componentWillReceiveProps() {}
  UNSAFE_componentWillUpdate() {}
}

References