react/no-unsafe

Disallow the UNSAFE_ lifecycle methods of class components

UNSAFE_componentWillMount, UNSAFE_componentWillReceiveProps, and UNSAFE_componentWillUpdate run before React commits a render. React can start a render, drop it, and start again, so these methods can run several times for one update. If you start a request or a subscription there, it can start several times and may never be cleaned up.

Instead ofUse
UNSAFE_componentWillMountcomponentDidMount
UNSAFE_componentWillReceivePropsgetDerivedStateFromProps
UNSAFE_componentWillUpdatecomponentDidUpdate

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"react/no-unsafe" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"react/no-unsafe" = ["error", {
  checkAliases = false,
}]

Examples#

example.jsx1 problem
class Feed extends React.Component {
  UNSAFE_componentWillMount() {
    this.subscription = subscribe(this.props.channel);
  }
}
example.jsxNo problems
class Feed extends React.Component {
  componentDidMount() {
    this.subscription = subscribe(this.props.channel);
  }
}

Options#

OptionDefaultWhat it does
checkAliasesfalseAlso report the old names without the prefix: componentWillMount, componentWillReceiveProps, and componentWillUpdate.
example.jsxwith { "checkAliases": true }1 problem
class Feed extends React.Component {
  componentWillMount() {
    this.subscription = subscribe(this.props.channel);
  }
}