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 of | Use |
|---|---|
UNSAFE_componentWillMount | componentDidMount |
UNSAFE_componentWillReceiveProps | getDerivedStateFromProps |
UNSAFE_componentWillUpdate | componentDidUpdate |
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() {UNSAFE_componentWillMount is unsafe for use in async rendering. Update the component to use componentDidMount instead. See reactjs.org/blog/2018/03/27/update-on-async-rendering.html.
this.subscription = subscribe(this.props.channel);
}
}example.jsxNo problems
class Feed extends React.Component {
componentDidMount() {
this.subscription = subscribe(this.props.channel);
}
}Options#
| Option | Default | What it does |
|---|---|---|
checkAliases | false | Also report the old names without the prefix: componentWillMount, componentWillReceiveProps, and componentWillUpdate. |
example.jsxwith
{ "checkAliases": true }1 problemclass Feed extends React.Component {
componentWillMount() {componentWillMount is unsafe for use in async rendering. Update the component to use componentDidMount instead. See reactjs.org/blog/2018/03/27/update-on-async-rendering.html.
this.subscription = subscribe(this.props.channel);
}
}